IT.COM

[Resolved] PHP Question

Spaceship Spaceship
Watch

Archangel

randypendleton.comTop Member
Impact
1,768
PHP Question

Simple question: Can I use replace() in PHP to change text in a document? If not, does anyone know how to go about this?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
1
•••
Eric said:
replace? No, but you can use www.php.net/str_replace


I'm not too good at PHP. Sadly, this kinda confuses me. Let's say I want to replace all instances of site1.com with site2.com. Exactly how would I write that out? This function seems to require 3 parameters...
 
0
•••
PHP:
$string = "I'm not too good at PHP. Sadly, this kinda confuses me. Let's say I want to replace all instances of site1.com with site2.com. Exactly how would I write that out? This function seems to require 3 parameters...";

$string = str_replace(array('site1.com', 'site2.com'), '', $string);

Archangel said:
I'm not too good at PHP. Sadly, this kinda confuses me. Let's say I want to replace all instances of site1.com with site2.com. Exactly how would I write that out? This function seems to require 3 parameters...
 
0
•••
Eric said:
PHP:
$string = "I'm not too good at PHP. Sadly, this kinda confuses me. Let's say I want to replace all instances of site1.com with site2.com. Exactly how would I write that out? This function seems to require 3 parameters...";

$string = str_replace(array('site1.com', 'site2.com'), '', $string);


Looks good but I need to ask: What is $string referring to the top text? Is that the text that I want the script to look through & make the necessary replaces to? lol I know a little PHP but this is something outta my league.
 
0
•••
Archangel said:
Looks good but I need to ask: What is $string referring to the top text? Is that the text that I want the script to look through & make the necessary replaces to? lol I know a little PHP but this is something outta my league.
Ah, yes - it refers to what text you're wanting PHP to perform the replacements on.
 
0
•••
Eric said:
Ah, yes - it refers to what text you're wanting PHP to perform the replacements on.

Thanks for your help on this :) Reps added
 
0
•••
No problem, and thanks. :tu:
 
0
•••
is it just me or does $string = str_replace(array('site1.com', 'site2.com'), '', $string); just remove all instances of site1.com AND site2.com... i thought he said he had wanted to convert site1.com into site2.com?

in which case you'd have to do: $string = str_replace('site1.com', 'site2.com', $string);
 
1
•••
nasaboy007 said:
is it just me or does $string = str_replace(array('site1.com', 'site2.com'), '', $string); just remove all instances of site1.com AND site2.com... i thought he said he had wanted to convert site1.com into site2.com?

in which case you'd have to do: $string = str_replace('site1.com', 'site2.com', $string);
Ah, good catch - I read it as he wanted to replace them both. :red:
 
0
•••
nasaboy007 said:
is it just me or does $string = str_replace(array('site1.com', 'site2.com'), '', $string); just remove all instances of site1.com AND site2.com... i thought he said he had wanted to convert site1.com into site2.com?

in which case you'd have to do: $string = str_replace('site1.com', 'site2.com', $string);

I just tested the code out and it ran albeit not the way I intended. But it's cool -- I think I could have done better had I typed my replies a bit clearer :)
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back