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

4
  • Runs fine. Are you sure you don't have any special characters (such as new lines) within the HTML? Also, if this comes from a webpage, you should consider DOM or methods of parsing. Commented Mar 12, 2014 at 3:59
  • 3
    Works fine here. Are you viewing this in a browser? Try 'view source', in case the browser is hiding the tag from view. Commented Mar 12, 2014 at 3:59
  • @MarcB I swear that's probably what the OP is experiencing, good call. Commented Mar 12, 2014 at 4:00
  • Thanks guys, that's exactly the problem, I'm testing with Chrome browser. Commented Mar 12, 2014 at 4:07

3 Answers 3

4
$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;
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.