Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

str_replace construct 1

Status
Not open for further replies.

Dashley

Programmer
Dec 5, 2002
925
US
Hi,

$homepage = file_get_contents("echo $homepage;


in $homepage there is a image source statement I'm trying to replace with nothing "" and then display the page without the image.
the statement is:

<img SRC=" BORDER="0" alt="Bargain, Overstock, and Remainder Book News" hspace="6" vspace="1" align="top" >


I'm using the str_replace function but am having absolutely zero success. I've tried every way to present this string into the function for replacement. I already have it working in asp.net (VB) but need to get it working in PHP. I've tried double double quoting, single quotes, apostrophes etc..

Any help would be greatly appreciated. :)

Thanks
 
let's presume you have the legal right to scrape content like this.

Code:
$homepage = file_get_contents("[URL unfurl="true"]http://www.imakenews.com/eletra/mod_archive_view.c...");[/URL]
$find = 'bargainbook_news.gif';
$homepage = preg_replace('|http\:[^"]*?bargainbooknews_logo\.GIF\"|i', '"', $homepage);

if you're doing anything else more detailed then you might consider phpQuery which can traverse the DOM tree and allow you to change attributes on the fly.
 
Hi

Justin, was your answer cut/truncated in some way ? $find is just initialized but not used. And generally I can not get its goal the way it currently is.


Feherke.
feherke.ga
 
ah no. i was going to do it stepwise for more obvious review. i.e. incorporate $find in the pattern. i forgot to delete it when i went for the all in one route. it serves no purpose other than looking pretty.

where is that edit functionality we've all been asking for for ten years ....
 
Hi

Ok, then. Just got confused by the unnecessary escaping of colon ( : ) and double quote ( " ). They gave me the feeling that something else is missing.


Feherke.
feherke.ga
 
I use a regex editor and i'm never sure what flavour of regex it is set to, so I tend to escape anything that I think might be a control character and then tidy up for the particular flavour I am working for.

agreed that unnecessary for php.
 
Thank you very much jpadie.

You've presumed correctly. :) I'm a dot net developer. Been using basic since Bill Gates put it out on his MS DOS disc's.

Well a few days ago I had this project politely dropped on my desk :).
It is a wordpress site which I've never used before and of some PHP is needed. I once tried out PHP when it first came out but
opted to stay with VB. So to say the least I'm having fun, not :).

When I saw this " [^"]*? " or you pipes | I went out searching the PHP documentation for it. Still looking.

Thanks again for your response. It works just fine.




 
Hi

Just to give you an extra keyword for your searches, the "p" in [tt]preg_replace()[/tt] stands for PCRE ( Perl-Compatible Regular Expressions ), a regular expression flavor.


Feherke.
feherke.ga
 
I use the pipes to delineate the start and end of the regular expression. you can use 'anything'... but there are limitations. many people use forward slash.

the square brackets denote a character class. i.e. any character in the square brackets, rather than all characters sequentially. the caret to start with negates the class. so NO characters in the square brackets...

the asterisk means 1 or more repetitions and the question mark makes the search 'un-greedy'. i.e. it stops the first time the next character is reached.

this is pretty standard posix compliant regular expressions. similar syntax exists in VB.
 
Wow,

Thank you for the detailed explanation. I've never had to do much string manipulation.
Mainly basic searches and at the DB level.

I've went to the web and found a great page for posix compliant regular expressions.
I've also went and read up on the preg_replace function. 1st time seeing it. Nice function.


Thank again !






 
and of course Feher's right when he says that the P of PCRE is a reference to perl and not posix. far too tired today ....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top