ira19 Posted April 24, 2006 Share Posted April 24, 2006 Hi, I want to search for £ 14.23 i.e £ followed by decimal numbers occuring in a string. Can someone tell me how to do it?Regards,Ira Quote Link to comment https://forums.phpfreaks.com/topic/8242-search-for-word-in-a-string/ Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 Set the $needle variable to whatever you're looking for in the string.$haystack should be replaced with whatever the variable of your string is.This will return $needle_location as position of the needle in the haystack.From there, you can use a substr() function to chop down the haystack into a chuck appropriate for what you want.[code]$needle = "£";$needle_location = strpos($haystack, $needle);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8242-search-for-word-in-a-string/#findComment-30041 Share on other sites More sharing options...
ira19 Posted April 24, 2006 Author Share Posted April 24, 2006 But this will give me only the occurence of the first £ , I need to get all the £ appearing in the string, how do i do that :( ? Quote Link to comment https://forums.phpfreaks.com/topic/8242-search-for-word-in-a-string/#findComment-30047 Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 ok, thats a bit more complex and i'll have to test my code. I will go and run some tests and try and loop through a longer string and see if i can exctract them all.brb Quote Link to comment https://forums.phpfreaks.com/topic/8242-search-for-word-in-a-string/#findComment-30048 Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 Run this code as is, and you'll see what it does. Any more help you might need, just yell.[code]<?php$needle = "£";$string = "If it costs £252.62 to buy something that should cost £300.00, you've bagged yourself a bargain and got it for £47.38 cheaper than it should have been.";echo "<b>Original String:</b><br>$string<br><br>";$number_of_occurances = substr_count("$string", "$needle");echo "$needle appears $number_of_occurances times.<br><br>";$count = 0;while ($count<$number_of_occurances){$string = substr($string, strpos("$string", "$needle")+1);echo "$string<br><br>"; $count++;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8242-search-for-word-in-a-string/#findComment-30067 Share on other sites More sharing options...
ira19 Posted April 25, 2006 Author Share Posted April 25, 2006 [!--quoteo(post=367944:date=Apr 24 2006, 12:30 PM:name=wisewood)--][div class=\'quotetop\']QUOTE(wisewood @ Apr 24 2006, 12:30 PM) [snapback]367944[/snapback][/div][div class=\'quotemain\'][!--quotec--]Run this code as is, and you'll see what it does. Any more help you might need, just yell.[code]<?php$needle = "£";$string = "If it costs £252.62 to buy something that should cost £300.00, you've bagged yourself a bargain and got it for £47.38 cheaper than it should have been.";echo "<b>Original String:</b><br>$string<br><br>";$number_of_occurances = substr_count("$string", "$needle");echo "$needle appears $number_of_occurances times.<br><br>";$count = 0;while ($count<$number_of_occurances){$string = substr($string, strpos("$string", "$needle")+1);echo "$string<br><br>"; $count++;}?>[/code][/quote]Thanks a lot...the code is great!!!! Quote Link to comment https://forums.phpfreaks.com/topic/8242-search-for-word-in-a-string/#findComment-30429 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.