1
votes

the target text is something like:

Lorem ipsum dolor #sit# amet, consectetur #adipisicing# elit, sed do #eiusmod# tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip.

the coresponding data to hashtagged #sit#, #adipisicing#, #eiusmod# would be array like:

array(
  '42' => 'sit somewhere',
  '43' => 'adipisicing...',
  '63' => 'eiusmod...'
)

i want to use preg_replace or preg_replace_callback to replace the hashtagged by the array value in sequence. to get result as:

Lorem ipsum dolor <a href="/link/to/42" title="sit somewhere">#sit#</a> amet, consectetur <a href="link/to/43" title="adipisicing...">#adipisicing#</a> elit, sed do <a href="/link/to/63" title="eiusmod...">#eiusmod#</a> tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip.

how could that be achieved?

2

2 Answers

0
votes
$search = array(
    '#sit#',
    '#adipisicing#',
    '#eiusmod#'
    );
$replacement = array(
    '42' => 'sit somewhere',
    '43' => 'adipisicing...',
    '63' => 'eiusmod...'
    );
$str = str_replace($search, $replacement, $str);
0
votes

You are probably better off using str_replace.

See http://www.php.net/manual/en/function.str-replace.php