0103media Site Admin
Joined: 17 Feb 2005 Posts: 21
|
Posted: Thu Apr 14, 2005 12:08 pm Post subject: replace a string in php from point x to dynamic point y |
|
|
Here is a great little script I just wrote to remove the rss feeds url...
I am using a rss feed from yahoo news to add the lastest news on one of me site but the url contains the query plus the results from yahoo...
I think it look naff therefore I remove the yahoo url...
Yahoo uses a * to point out the end url
E.g. -us.rd.yahoo.com/dailynews/rss/search/Mobile+Computing/SIG=11jlvhne7/*-biz.yahoo.com/bw/050414/136010.html?.v=1
When I want the url to directly point to the news item like so :
-biz.yahoo.com/bw/050414/136010.html?.v=1
So how do you replace a string in php from point x to point y when you know point x if = to 0 and y = "*"...
First of all you need to find the position of a char in a string php
You can do this with the following code...
| Code: | $mystring = 'http://us.rd.yahoo.com/dailynews/rss/search/Mobile+Computing/SIG=11jlvhne7/*http://biz.yahoo.com/bw/050414/136010.html?.v=1';
$pos = strpos($mystring, "*");
if ($pos === false) { // note: three equal signs
echo $pos.'not here'; // not found...
}
echo $pos.' here '; // not found... |
The returns here 75 ... the 75th char in the string...
Now that you know the position of the char you can replace from 0 to 75.
The full code is below...
Don't forget to replace the char count number plus 1 to include that character....
Hope this helps..
| Code: | <?php
$mystring = 'http://us.rd.yahoo.com/dailynews/rss/search/Mobile+Computing/SIG=11jlvhne7/*http://biz.yahoo.com/bw/050414/136010.html?.v=1';
$pos = strpos($mystring, "*");
if ($pos === false) { // note: three equal signs
echo $pos.'not here'; // not found...
}
echo $pos.' here'; // not found...
$mystring = substr_replace($mystring, '', 0,$pos+1);
echo '\n\n string is'.$mystring;
?> |
|
|