Mohammad Alipour
Mohammad Alipour

Reputation: 313

How to replace a word with a php string with str_replace or preg_replace

How to replace a word with a php string with str_replace or preg_match? my code is :

$html = crawl_website($self_url."linkboxmain.php");
$getout = "usrid:<?=$_GET['tag'] ?>";
$string = str_replace("usrid:",$getout,$html);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/linkboxshow.php', $string);

above code not work!!

I want to replace "usrid:" with "usrid:<?=$_GET['tag'] ?>" and then put it to linkboxshow.php file.

crawl_website() is a function to get html of a page.

Upvotes: 0

Views: 199

Answers (2)

chris85
chris85

Reputation: 23880

The $_GET gets evaluated as a variable, escape or enclose it in single quotes and should be good to go...

'usrid:<?=$_GET[\'tag\'] ?>';

Also might be worth checking out http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double. because there are a few ways to go..

Upvotes: 1

Jade Kallo
Jade Kallo

Reputation: 110

Try it like this:

$html = crawl_website($self_url."linkboxshow.php");
$getout = "usrid:".isset($_GET['tag']) ? $_GET['tag'] : ''."";
$string = str_replace("usrid:",$getout,$html);

Upvotes: 1

Related Questions