2

This is my code:

$string = '<a href="http://www.mysite.com/test" class="prevlink">&laquo; Previous</a><a href=\'http://www.mysite.com/test/\' class=\'page\'>1</a><span class=\'current\'>2</span><a href=\'http://www.mysite.com/test/page/3/\' class=\'page\'>3</a><a href=\'http://www.mysite.com/test/page/4/\' class=\'page\'>4</a><a href="http://www.mysite.com/test/page/3/" class="nextlink">Next &raquo;</a>';
$string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
$string = preg_replace('@(&lt;a).*?(nextlink)@s', '', $string);
    echo $string;

I am trying to remove the last link:

<a href="http://www.mysite.com/test/page/3/" class="nextlink">Next &raquo;</a>';

My current output:

">Next &raquo;</a>

It removes everything from the start. I want it to remove only the one with strpos, is this possible with preg_replace and how? Thanks.

10
  • Trying to use regex for html (or xml) can be quite a deal. In your case you say to replace something that starts with <a has some characters in the middle end ends with nextlink etc. regex try to make the bigger match the can. So <a ....>..<a .... >..<a ..nextlink... is a valid match. Aug 31, 2011 at 8:29
  • stackoverflow.com/questions/1732348/…
    – user554546
    Aug 31, 2011 at 8:34
  • @George $string = preg_replace('@(&lt;a).*?(nextlink).*?(&lt;/a&gt;)@s', '', $string); The output is empty. Great idea, I think my prex statment is wrong. could you take a look. Thanks
    – Muazam
    Aug 31, 2011 at 8:45
  • You said you want to remove the last link... do you mean you want the final output to be <a href="http://www.mysite.com/test" class="prevlink">&laquo; Previous</a><a href=\'http://www.mysite.com/test/\' class=\'page\'>1</a><span class=\'current\'>2</span><a href=\'http://www.mysite.com/test/page/3/\' class=\'page\'>3</a><a href=\'http://www.mysite.com/test/page/4/\' class=\'page\'>4</a>?
    – pb149
    Aug 31, 2011 at 8:53
  • @Pete171 Correct, that's what I want. The first one. I want to remove <a href="http://www.mysite.com/test/page/3/" class="nextlink">Next &raquo;</a>
    – Muazam
    Aug 31, 2011 at 8:53

2 Answers 2

1

quite a tricky question to solve

first off, the .*? will not match like you are expecting it to.

its starts from the left finds the first match for <a, then searches until it finds nextlink, which is essentially picking up the entire string.

for that regex to work as you wanted, it would need to match from the righthand side first and work backwards through the string, finding the smallest (non-greedy) match

i couldn't see any modifiers that would do this so i opted for a callback on each link, that will check and remove any link with nextlink in it

<?php
$string = '<a href="http://www.mysite.com/test" class="prevlink">&laquo; Previous</a><a href=\'http://www.mysite.com/test/\' class=\'page\'>1</a><span class=\'current\'>2</span><a href=\'http://www.mysite.com/test/page/3/\' class=\'page\'>3</a><a href=\'http://www.mysite.com/test/page/4/\' class=\'page\'>4</a><a href="http://www.mysite.com/test/page/3/" class="nextlink">Next &raquo;</a>';

echo "RAW: $string\r\n\r\n";

$string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');

echo "SRC: $string\r\n\r\n";


    $string = preg_replace_callback(
        '@&lt\;a.+?&lt;/a&gt;@',
        'remove_nextlink',
        $string
    );


function remove_nextlink($matches) {

    // if you want to see each line as it works, uncomment this
    // echo "L: $matches[0]\r\n\r\n";

    if (strpos($matches[0], 'nextlink') === FALSE) {
        return $matches[0]; // doesn't contain nextlink, put original string back
    } else {
        return ''; // contains nextlink, replace with blank
    }
}    

echo "PROCESSED: $string\r\n\r\n";
2
  • Thanks for the explanation, I have to learn pregEx it seems very powerful. The code works, thanks a lot. =)
    – Muazam
    Aug 31, 2011 at 9:40
  • it's not the most elegant solution, but it does work, and writing regex to work on html is always hard work
    – bumperbox
    Aug 31, 2011 at 9:43
1

Note: This is not a direct answer, but a suggestion to another approach.

I was told once; if you can do it in any other way, stay away from regex. I don't though, it's my white whale. Have you heard of phpQuery? It's jQuery implemented in PHP and very powerful. It would be able to do what you want in a very easy way. I know it's not regex, but perhaps it's of use to you.

If you really want to go ahead, I can recommend http://gskinner.com/RegExr/ . I think it's a great tool.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.