im trying to find a way to use preg_replace_callback to replace the matches with <b>
match </b>
for example :
the text :
This is a text ,, text , this is a text
the pattern
/text/ or /(text)/ or any pattern which can find text
i want the result is:
This is a
<b>
text</b>
,,<b>
text</b>
, this开发者_高级运维 is a<b>
text</b>
Thanks..
Do you mean something like
(\btext\b)
and replace it with
<b> $1 <\b>
See it here on Regexr
I don't think that you need preg_replace_callback
, you can use backreferences with preg_replace
that should be enough for what you want.
You can do something like
$string = 'This is a text ,, text , this is a text';
$pattern = '/(\btext\b)/';
$replacement = '<b> $1 <\b>';
echo preg_replace($pattern, $replacement, $string);
See the preg_replace doc
What's wrong with:
<?php
$search = 'test';
$text = 'This is a test string';
// Filter out unwanted junk
$search = preg_replace('/[^a-z0-9]/', '', $search);
echo preg_replace('/('.$search.')/i', '<b>\1</b>', $text);
?>
This would produce:
This is a <b>test</b> string
Why don't you try this.
$string = "This is a text ,, text , this is a text";
$string = str_replace("text","<b>text</b>",$string);
Its very easy way, less confusion :)
精彩评论