-6

this is something that should be so simple, but Ive run this code in excess of 90 times trying to figure it out. Any help would be greatly appreciated!

Here is my entire function for context. I am trying to replace

<a href="04.htm">

with

<a style="cursor:pointer;" onclick="gotoScrollExercise(04); return false;">

the function:

       $replacethis = '(<a href=\"([0-9]{2})\\.htm\">)';
   $linkArr = [];

   $lessonNo = preg_match_all($replacethis, $originalcontent, $linkArr);

   for($x = 0; $x < count($linkArr[1]); $x++) {
        $replacethis = '<a href="'.$linkArr[1][$x].'.htm">';
        $replacewith = '<a style="cursor:pointer;" onclick="gotoScrollExercise('. $linkArr[1][$x] .'); return false;">';
        $originalcontent = str_replace($replacethis, $replacewith, $originalcontent);
    }

If I change the $replacewith variable to something simple, I have no problems but as soon as I add an onclick function, it just ignores that part of the string.

example that works:

$replacewith = '<a id="blah" style="cursor:pointer;">';

Examples that dont work:

$replacewith = '<a fdgdfgdf>';
$replacewith = '<a onclick="function()">';

why?

reproduceable example:

$replacethis = '<a href="04.htm">';
$replacewith = '<a style="cursor:pointer;" onclick="gotoScrollExercise(04); return false;">';
$originalcontent = str_replace($replacethis, $replacewith, $originalcontent);

output is:
<a style="cursor:pointer;">
olstearn
  • 1
  • 2
  • 2
    you have a single quoted string, so you need to escape single quotes. you are escaping the double quotes, which you should not be doing in a single quoted string. escape the single quotes. – chiliNUT Jun 21 '16 at 15:18
  • 1
    you're starting your loop from `0`, and a capture group in regexes always makes the capture results array [0] index be the ENTIRE matched string. e.g. `var_dump($linkArr)` to see what you got. – Marc B Jun 21 '16 at 15:18
  • thanks for the suggestion marc, but its nothing to do with the loop. Im happy with the output from the regex. and when i error_log the replacewith, it is exactly how i want it. It mustbe the strreplace which is not happy. – olstearn Jun 21 '16 at 15:23
  • chilinut, why do I have to escape anything? it is a single quoted string and all the quotes in the html are doubles. – olstearn Jun 21 '16 at 15:25
  • Please give a full, reproducible example of one `str_replace` that does not give the desired result, providing the values of the arguments you provide it. Saying *"it just ignores that part of the string"* or *"that don't work"* is not very clear. Just give the output you get and the output you want together with the provided input. – trincot Jun 21 '16 at 15:29
  • You just changed it. Before it wasn't single quotes – B001ᛦ Jun 21 '16 at 15:29
  • yes, apologies, I realised when I typed out the examples I had put double quotes around them. – olstearn Jun 21 '16 at 15:35

1 Answers1

0

i took exactly what you wrote as code and tried to replace it. for me it works..

working example

so, there posibly could be some typo or forgotten something which you didn't brought here. Check that you have enabled error reporting (here is how) and that you use same code like mine in the demo

Community
  • 1
  • 1
Jimmmy
  • 579
  • 12
  • 26
  • thank you for taking the time to help. All I can think is that this was a php version issue. I gave up and uploaded the script as is to the server and it worked. Php version 5.5.1 seemed to be responsible. No such issues with 5.6.2 – olstearn Jun 22 '16 at 08:38