2

My script extract span with it's attributes. I want to replace span with attributes with something else. I am using following piece of code, maybe cause of special characters it doesn't work.

    $tag=strtolower(str_replace("'<<span_id="ctl00_cphpagecontant_label6"><b>'","B",$tag));
6
  • So you want to replace the exact string '<<span_id="ctl00_cphpagecontant_label6"><b>' with B referenced as $tag? Commented Dec 24, 2011 at 8:16
  • @jensgram Yes, to replace exact string with B Commented Dec 24, 2011 at 8:17
  • In that case the above should work. Can you paste an example of $tag's value before attempting the above? My gut-feeling is that the string is not exactly what you want to replace (<<, contant and wrapped in 's all seem a little weird). Commented Dec 24, 2011 at 8:17
  • @jensgram I think the value is not in $tag <<span_id="ctl00_cphpagecontant_lalel1"><b>job_categories</b></span>><span id="ctl00_cphPageContant_FunctionalAria"><font size="2">Consultancy</font></span></<span_id="ctl00_cphpagecontant_lalel1"><b>job_categories</b></span>> Commented Dec 24, 2011 at 8:22
  • @jensgram I tried all but it was not working, finally I break the string into pieces and replace each separate. now trying to replace this <=""> with nothing. $tag=strtolower(str_replace("'<=\"\">'",""), $tag); Commented Dec 24, 2011 at 9:17

1 Answer 1

3

Nesting quotes, ya' need to escape 'em mate!

If you have a string wrapped in "" and would like this string to contain a literal " you will need some sort of method to say that "hey, I want this character here - but it's not the end of the string", that's where escaping comes in.

By prepending your character with a backslash \ you tell the PHP interpreter just that; this character is a part of the string, it's not used to denote the end of it.

$tag=strtolower (
  str_replace ("'<<span_id=\"ctl00_cphpagecontant_label6\"><b>'","B",$tag)
);

Note: Does the string you'd like to replace start and end with '? Otherwise those characters should be removed from the string.

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

1 Comment

I tried all but it was not working, finally I break the string into pieces and replace each separate. now trying to replace this <=""> with nothing. $tag=strtolower(str_replace("'<=\"\">'",""), $tag);

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.