Forum Moderators: coopster

Message Too Old, No Replies

trim a string.

         

zozzen

10:10 am on Feb 17, 2008 (gmt 0)

10+ Year Member



Hi i faced a problem about trimming a string.

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]

zozzen

10:24 am on Feb 17, 2008 (gmt 0)

10+ Year Member



corrected:

....only article_id is useful to me and the rest is irrelevant. sorry for the typo.

RonPK

12:35 pm on Feb 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could either use a combination of substr() [php.net] and strrpos() [php.net], or preg_replace() [php.net] with a regular expression.

zozzen

1:05 pm on Feb 17, 2008 (gmt 0)

10+ Year Member



thanks for the hints! Regular expression is very useful.

eregi_replace("http://www.example.com/[a-z]/[a-z]","http://www.example.com",$output);

[edited by: dreamcatcher at 1:33 pm (utc) on Feb. 17, 2008]
[edit reason] Use example.com, thanks. [/edit]

jatar_k

2:06 pm on Feb 17, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if the urls are always in that form you could just split [php.net] it on =

PHP_Chimp

4:55 pm on Feb 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or if you just want to keep the last few numbers but there may be a different splitting character then -

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.

dragon master mokuba

8:40 am on Feb 18, 2008 (gmt 0)

10+ Year Member



here is a little function i use in some of my coding.


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

and to use it:

$sample = "http://www.example.com/cook/french/article_id=001"; // input string
$sample2 = processText($sample);
echo $sample2;

zozzen

3:41 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



Thanks for all inspirations here. s a beginner i'm really exhausted with the fact that the same task can be done by so many different function, but it makes php more interesting to me. Thanks again!