gregaryb Posted October 31, 2023 Share Posted October 31, 2023 I am at a total loss as to why this is causing an infinite loop. I just don't understand what it it that strpos is returning when it fails to find the sibstring! How do I make this loop work? function StrReplace($strText, $strFind, $strReplace) { $nI = strpos($strText, $strFind); if ($nI !== FALSE) { $strLeft = substr($strText, 0, $nI); $strRight = substr($strText, $nI + 1); $strText = $strLeft . $strReplace . $strRight; echo "#############<br>"; echo "strText=".$strText."<br>"; echo "strLef =".$strLeft."<br>"; echo "strRight=".$strRight."<br>"; echo "nI=".$nI."<br>"; echo "#############<br>"; } return $strText; } function StrFind($strText, $strFind) { $nPos = strpos($strText, $strFind); if ($nPos === FALSE) $nPos = -1; return $nPos; } function EscapeQuotes($strText) { if (is_string($strText)) { StrFind($strText, "'"); while (StrFind($strText, "'")) { $strText = StrReplace($strText, "'", "#"); echo "@@@@@@@@@@@@@@@@@@@@<br>"; echo $strText."<br>"; echo "@@@@@@@@@@@@@@@@@@@@<br>"; } } } Why is my function StrFind(...) always returning true or greater that 1? How do I stop it from doing that? Quote ############# strText=Greg#s Native Landscapes strLef =Greg strRight=s Native Landscapes nI=4 ############# @@@@@@@@@@@@@@@@@@@@ Greg#s Native Landscapes @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ Greg#s Native Landscapes @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ Greg#s Native Landscapes @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ Greg#s Native Landscapes @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ Greg#s Native Landscapes @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ Greg#s Native Landscapes @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ Greg#s Native Landscapes @@@@@@@@@@@@@@@@@@@@ Link to comment Share on other sites More sharing options...
Don E Posted October 31, 2023 Share Posted October 31, 2023 The while loops keeps running because the condition is always TRUE. Try: $pos = StrFind($strText, "'"); //this is position 4 while($pos) //$pos starts as 4 { $strText = StrReplace($strText, "'", "#"); echo "@@@@@@@@@@@@@@@@@@@@<br>"; echo $strText."<br>"; echo "@@@@@@@@@@@@@@@@@@@@<br>"; $pos--; //this decrements $pos in each loop until it becomes zero which is FALSE and the loop should stop. } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now