web design forum Forum Index web design forum
Design Web Page HTML Free Web Design Web Designers Learn HTML Web Design Tips Free Web Design Software HTML Help Web Development HTML Guide forum uk
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

replace a string in php from point x to dynamic point y

 
Post new topic   Reply to topic    web design forum Forum Index -> PHP help
View previous topic :: View next topic  
Author Message
0103media
Site Admin


Joined: 17 Feb 2005
Posts: 21

PostPosted: Thu Apr 14, 2005 12:08 pm    Post subject: replace a string in php from point x to dynamic point y Reply with quote

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;

?>
Back to top
View user's profile Send private message Send e-mail
webcre



Joined: 28 Nov 2005
Posts: 5
Location: UK

PostPosted: Mon Nov 28, 2005 2:35 pm    Post subject: Reply with quote

Nice one Ive needed something like that before. I'll keep it noted.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    web design forum Forum Index -> PHP help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group