Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

str_replace construct

str_replace construct

str_replace construct


$homepage = file_get_contents("http://www.imakenews.com/eletra/mod_archive_view.c...");
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="http://www.imakenews.com/bargainbooknews/bargainbo..." 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. :)


RE: str_replace construct


If too many quotes in a string literal become confusing, try nowdoc.


RE: str_replace construct

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


$homepage = file_get_contents("http://www.imakenews.com/eletra/mod_archive_view.c...");
$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.

RE: str_replace construct


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.


RE: str_replace construct

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 ....

RE: str_replace construct


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


RE: str_replace construct

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.

RE: str_replace construct

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.

RE: str_replace construct


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


RE: str_replace construct

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.

RE: str_replace construct


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 !

RE: str_replace construct

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 ....

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close