Support » Plugins » Please help me fix this regex

  • Reuben


    I wrote a plugin named “Simple Smart Links” to replace an older no-longer-working wordpress plugin that I’d been relying on heavily (it makes links out of text like [title -> href]. My plugin relies on regex, and it’s not quote working properly.

    The code is:

    function replace_the_text($content) {
            $content = ereg_replace("\[([0-9a-zA-Z\(\)\'\"\? ]*)[ ]*\-\>[ ]*([0-9a-zA-Z:/\.\_\-\'\(\)\?\=]*)\]", '<a href="\2">'.trim('\1').'</a>', $content);
            return $content;
    add_filter('the_content', 'replace_the_text');

    The issue is that despite the trim, spaces sometimes end up in the link. Using this following test post:

    [hello -> foo] this is test 1.
    [hello->foo] this is test 2.
    [hello ->foo] this is test 3.
    [hello -> foo]
    [hello -> foo]

    …I end up with:

    <p><a href="foo">hello </a> this is test 1.<br />
    <a href="foo">hello</a> this is test 2.<br />
    <a href="foo">hello </a> this is test 3.<br />
    <a href="foo">hello </a><br />
    <a href="foo">hello </a></p>

    Notice that test 2 is the only one that doesn’t end up with spaces. So the space before the arrow is being greedily matched, which is fair enough – but the trim is supposed to take care of that. Anybody shed any insight into what’s going on?


Viewing 3 replies - 1 through 3 (of 3 total)
  • Dagon Design


    The problem is that in the first grouping (for the ‘hello’), you are including a space in the list of possible characters. That means it will grab the space after the ‘hello’.

    You cannot use trim like that, as all you are doing is trimming the string ‘\1’ and not what it will be replaced with.

    Change the line to this:

    $content = ereg_replace("\[([0-9a-zA-Z\(\)\'\"\?]*)[ ]*\-\>[ ]*([0-9a-zA-Z:/\.\_\-\'\(\)\?\=]*)\]", '<a href="\2">\1</a>', $content);

    And it will work, since it will no longer allow spaces in the first group. It will be handled by the [ ]* instead.



    Unfortunately I can’t remove that space, as I need to allow for links like: “This awesome page”.

    The \1 is clearly wrapped by a href after the plugin runs; does PHP only replace it after the entire line has evaluated?

    Is there alternate syntax where I could break the match into groups and then operate on them individually before replacing them in the main content?





Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Please help me fix this regex’ is closed to new replies.