Question

<?php

$html = '<form id="form1" name="f1" action="/jk" dummy';

$html = str_replace(
    '<form id="form1" name="f1"',
    '<form id="form1" name="f1" xxxxxxxxxxxxx',
    $html);

echo $html;

Why the result is empty?

I'm using PHP 5.3.27 (cli)

Thanks.

Was it helpful?

Solution

$html = '<form id="form1" name="f1" action="/jk" dummy';
$html = str_replace(
    '<form id="form1" name="f1"',
    '<form id="form1" name="f1" xxxxxxxxxxxxx',
    $html);

echo highlight_string($html, true);

another way to show is:

header("Content-Type: text/plain");
echo $html;

OTHER TIPS

Try this method,

<?php
echo 'hello<br>';
$html = "abcde<br>";
echo $html;
$a = substr_replace($html,
    'xyzhi',
    0);

echo $a;
?>

Str_replace will need word which you want to replace from the string.I hope this may work.

You can look at source code ctr+u you will find it is replaced successfully there, but Why cannot see it on the browser page? because the browser thinks of it as a tag. So to show it on the browser page we can use htmlentities():

$html = '<form id="form1" name="f1" action="/jk" dummy';
$html = str_replace(
    '<form id="form1" name="f1"',
    '<form id="form1" name="f1" xxxxxxxxxxxxx',
    $html);

echo htmlentities($html);

Output:

<form id="form1" name="f1" xxxxxxxxxxxxx action="/jk" dummy
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top