Forum Moderators: coopster

Message Too Old, No Replies

Php script for searching number.

         

almon

2:54 pm on Jan 23, 2009 (gmt 0)

10+ Year Member



I have a string(a paragraph) in which there is a number.I have to extract this number.How can i do this...after this I want to do calculation on this number...Help pls!

And yes one more thing...can I search the string with a start and ending tag? for example....suppose i want to search line which start with "al" and ends with "kum"

Thanks in advance

Shores

3:58 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



You should try string functions such as strpos and/or regular expressions.

If the number is the only number in the string, you can also try with intval.

Bye!

coopster

7:00 pm on Jan 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, almon.

A regular expression is likely to be your best option. intval [php.net] is not going to work as you expect ...

$string = 'My string has 123 in the middle'; 
var_dump(intval($string)); // prints int(0)
exit;

Morgenhund

11:15 pm on Jan 27, 2009 (gmt 0)

10+ Year Member



hmmm... to extract number (only the first number found, you have to use a callback if you need more):

$my_string = 'Here is my 12345 number...';

if (preg_match('/(\d+)/', $my_string, $matches)) {

echo "First number found: {$matches[1]}";

} else {

echo "No numbers found, sorry!";

}

To extract al...kum matching strings:

$my_string = 'lorem ipsm dolor sit amet altestkum';

if (preg_match('/(al.+kum)/', $my_string, $matches)) {

echo "Something found: {$matches[1]}";

} else {

echo "No matches found, sorry!";

}

whoisgregg

11:43 pm on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you need to match negative numbers or decimal numbers in addition to just integers, you could use a regular expression like this one:

echo '<pre>';
echo $my_string = 'Here is my -123.45 number, and a 35-number, finally a -0.5...';
if (preg_match_all('/(-?[0-9]+(\.?[0-9]+)?)/', $my_string, $matches)) {
echo PHP_EOL.'First number found: '.$matches[0][0].PHP_EOL;
print_r($matches[0]);
} else {
echo "No numbers found, sorry!";
}

almon

2:11 pm on Jan 29, 2009 (gmt 0)

10+ Year Member



Let me tell you what I am actually trying to do.
I want to extract Html code from an domain and want to search number between <span class name="pr"> and </span class>.
can anyone tell me the php script for this purpose?

almon

2:12 pm on Jan 29, 2009 (gmt 0)

10+ Year Member



It urgent pls!

almon

2:14 pm on Jan 29, 2009 (gmt 0)

10+ Year Member



And yes....I have this script but It gets the contents but it doesnt prints actual html code...The browser just parse the html and displays it

$my_string = file_get_contents($url);

coopster

2:40 pm on Jan 29, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That just brings the data into a string for you. That's your first step. Now you might consider writing a regular expression that will locate the pattern and capture the portion of the pattern which you seek. The Perl-compatible regular expression function preg_match [php.net] will likely be the appropriate tool in this case.