Forum Moderators: coopster
If I fetch a page, and get a sequence of strings like:
http://www.example.com/cook/french/article_id=001
http://www.example.com/cook/japanese/article_id=002
http://www.example.com/cook/english/article_id=003
http://www.example.com/deepfried/american/article_id=004
http://www.example.com/steam/abc/article_id=005
http://www.example.com/steam/dddd/article_id=006
Only article_id is useful to me and the rest is relevant, is there any function that i can ONLY keep the article_id?
Thanks a lot!
[edited by: dreamcatcher at 1:32 pm (utc) on Feb. 17, 2008]
[edit reason] Use example.com, thanks. [/edit]
preg_match('%(\d+)$%', $input, $matches);
echo $matches[1];
As this will match any numbers at the end of a string. So as long as the splitting character isnt another number then this will return the last numbers (so long as there is at least 1) from the end of the string.
<?php
function lastIndexOf($string,$item) {
$index=strpos(strrev($string),strrev($item));
if ($index)
{
$index=strlen($string)-strlen($item)-$index;
return $index;
}
else {
return -1;
}
}
function processText($txt) {
$length1 = lastIndexOf($txt,"article_id");
$length2 = strrev($txt);
$length3 = substr($length2, 0, -$length1);
$length4 = strrev($length3);
//uncomment the next two lines if you just want the number and change "return $length4" to "return $length6"
//$length5 = explode("=",trim($length4));
//$length6 = $length5[1];
return $length4;
}
?>
$sample = "http://www.example.com/cook/french/article_id=001"; // input string
$sample2 = processText($sample);
echo $sample2;