1

I have the following html entity codes:

$html = "<div id="sstssfb_aaa-theme_wrapper" class="first_sstform_wrapper"> <div class="bscon sstform_wrapper"> <div class="sst_formheaderbgr">"

I want to replace this string sstssfb_aaa-theme_wrapper with the new one using str_replace like so:

$num = 20;

$old = '#sstssfb_aaa-theme_wrapper';
$old = str_replace("#", "", $old);

$new = 'sstssfb_aaa-theme_wrapper' . $num;

$new_html = str_replace($old, $new, $html);

But it won't work at all and returning the original $html instead of the modified.

What would be the problem here and how to solve it?

EDIT:

It works after I trim the $old variable like this:

$old = trim(str_replace("#", "", $old));
7
  • 1
    please take a look at the last str_replace declaration... Commented Sep 23, 2015 at 6:37
  • 1
    Double quote the $html Commented Sep 23, 2015 at 6:38
  • agree with @SunilPachlangia because adding double quote on $html variable should solve the problem Commented Sep 23, 2015 at 6:41
  • The replacement works as expected. Your original code was faulty, you corrected it in between. $new_html contains the replaced token. Commented Sep 23, 2015 at 6:46
  • 1
    @arkascha is right, your code is working, don't see the issue. Commented Sep 23, 2015 at 6:58

1 Answer 1

1

Try this

$old = '#sstssfb_aaa-theme_wrapper';
$old = str_replace("#", "", $old);

$new = 'sstssfb_aaa-theme_wrapper' . $num;

$html = str_replace($old, $new, $html);

then printing the $html you will get the result.

Sign up to request clarification or add additional context in comments.

3 Comments

Would you care to elaborate the difference to the OPs code?
After replacing need to assign it to the variable $html
Correct answer for the original code the OP posted, though the OP has changed the code in between.

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.