Jump to content


Photo

Search for word in a string


  • Please log in to reply
5 replies to this topic

#1 ira19

ira19
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 24 April 2006 - 10:17 AM

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

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 10:45 AM

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.

$needle = "£";
$needle_location = strpos($haystack, $needle);

wisewood: proven fact, I am both wise, and wooden.

#3 ira19

ira19
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 24 April 2006 - 11:20 AM

But this will give me only the occurence of the first &pound , I need to get all the &pound appearing in the string, how do i do that :( ?


#4 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 11:23 AM

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
wisewood: proven fact, I am both wise, and wooden.

#5 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 12:30 PM

Run this code as is, and you'll see what it does. Any more help you might need, just yell.


<?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++;
}

?>

wisewood: proven fact, I am both wise, and wooden.

#6 ira19

ira19
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 April 2006 - 08:16 AM

[!--quoteo(post=367944:date=Apr 24 2006, 12:30 PM:name=wisewood)--][div class=\'quotetop\']QUOTE(wisewood @ Apr 24 2006, 12:30 PM) View Post[/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.
<?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++;
}

?>
[/quote]


Thanks a lot...the code is great!!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users