0

I use php preg-replace-callback function to add link to part of string that is in an array :

<?php
$preg_quote_titles =  array('string 1 ? string 2 ?');

$content="string 1 ? string 2 ? string 3 ? ";

    $content = preg_replace_callback( '/(' . implode( '|', $preg_quote_titles ) . ')/i', function( $matches ) use ( $preg_quote_titles ) {

            return '<a href="#" class="post-link">' . $matches[0] . ' </a>';

    }, $content );


    echo $content;
    ?>

Code above doesn't work. Result is :

string 1 ? string 2 ? string 3 ? 

With no link.

If I remove "?" character :

<?php
$preg_quote_titles =  array('string 1');

$content="string 1 ? string 2 ? string 3 ? ";

    $content = preg_replace_callback( '/(' . implode( '|', $preg_quote_titles ) . ')/i', function( $matches ) use ( $preg_quote_titles ) {

            return '<a href="#" class="post-link">' . $matches[0] . ' </a>';

    }, $content );


    echo $content;
    ?>

That works (links is added to "string 1")

How to include all character like parenthesis, question mark, etc ?

user2267379
  • 1,067
  • 2
  • 10
  • 20

0 Answers0