TeddyKiller Posted April 4, 2010 Share Posted April 4, 2010 Lets take a string.. $string = 'hello'; How would I check if that string, as the word "Re: " in it. If it doesn't, to add it on. So the output would be in this case Re: hello If we took this string.. $string = 'Re: hello'; Checked if it has Re: in it. It does have Re, in it.. so there for we don't add it on. Output would be.. Re: hello How can I do this? Thanks Link to comment https://forums.phpfreaks.com/topic/197535-how-would-i-check-if-a-string-has-a-word-in-and-add-it-on-if-it-doesnt-exist/ Share on other sites More sharing options...
TeddyKiller Posted April 4, 2010 Author Share Posted April 4, 2010 I've tried this.. $subject = $row['subject']; $pos = stripos('Re: ',$subject); if($pos !== false){ $subject = $subject; } else { $subject = 'Re: '.$subject; } Though it continues to add in Re: Link to comment https://forums.phpfreaks.com/topic/197535-how-would-i-check-if-a-string-has-a-word-in-and-add-it-on-if-it-doesnt-exist/#findComment-1036722 Share on other sites More sharing options...
litebearer Posted April 4, 2010 Share Posted April 4, 2010 Hmmm... <?php $haystack = "Subject"; $needle = "Re:"; $pos = strpos($haystack,$needle); if($pos === false) { // string needle NOT found in haystack echo "Not found: " . $needle . " in " . $haystack; } else { // string needle found in haystack echo "Not found: " . $needle . " in " . $haystack; } echo "<br><br>"; $row['subject'] = "Re: Something"; $haystack = $row['subject']; $needle = "Re:"; $pos = strpos($haystack,$needle); if($pos === false) { // string needle NOT found in haystack echo "Not found: " . $needle . " in " . $haystack; } else { // string needle found in haystack echo "found: " . $needle . " in " . $haystack; } echo "<br><br>"; ?> Link to comment https://forums.phpfreaks.com/topic/197535-how-would-i-check-if-a-string-has-a-word-in-and-add-it-on-if-it-doesnt-exist/#findComment-1036728 Share on other sites More sharing options...
TeddyKiller Posted April 4, 2010 Author Share Posted April 4, 2010 Ah cheers, can you explain why it never worked for me in the first place? Link to comment https://forums.phpfreaks.com/topic/197535-how-would-i-check-if-a-string-has-a-word-in-and-add-it-on-if-it-doesnt-exist/#findComment-1036734 Share on other sites More sharing options...
salathe Posted April 4, 2010 Share Posted April 4, 2010 Ah cheers, can you explain why it never worked for me in the first place? This line: $pos = stripos('Re: ',$subject); was searching for the position of $row['subject'] in the string "Re: ". In other words, your parameters were the wrong way around. See the stripos manual page. Link to comment https://forums.phpfreaks.com/topic/197535-how-would-i-check-if-a-string-has-a-word-in-and-add-it-on-if-it-doesnt-exist/#findComment-1036801 Share on other sites More sharing options...
TeddyKiller Posted April 4, 2010 Author Share Posted April 4, 2010 Ah cheers, can you explain why it never worked for me in the first place? This line: $pos = stripos('Re: ',$subject); was searching for the position of $row['subject'] in the string "Re: ". In other words, your parameters were the wrong way around. See the stripos manual page. Ah i understand. I thought having Re: infront of the string.. would look for the word infront, as if it was after it'd look if it was after the word. My mistake. Link to comment https://forums.phpfreaks.com/topic/197535-how-would-i-check-if-a-string-has-a-word-in-and-add-it-on-if-it-doesnt-exist/#findComment-1036870 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.