-2

I want to add a class to <p> if it matches following expression using preg_replace.

Following code does that however it cuts the first letter of the string (rtl language). Notice the word ہور, the first character ہ is being cut. How that can be fixed ?

$str = "<p>para 1</p><p> ہور سنا کنجڑا </p><p>para3</p>"; 
$result = preg_replace("~\p{Arabic}~u", "<p class=\"foo\">", $str, 1);

echo $result;

//output
<p>para 1</p><p> <p class="foo">ور سنا کنجڑا </p><p>para3</p>

Demo

sam
  • 1,027
  • 3
  • 11
  • 21
  • 1
    Kindly avoid [double postings](http://stackoverflow.com/questions/36018872/preg-match-all-paragraphs-in-a-string) (didn't one of the answers suit your needs?) – Jan Mar 15 '16 at 20:49
  • @Jan it was a completely different question, that's why I posted here as I remember reading in some other answer here that you should not ask multiple questions in same thread. – sam Mar 15 '16 at 20:56

2 Answers2

2

Group the found character.

$str = "<p>para 1</p><p> ہور سنا کنجڑا </p><p>para3</p>"; 
$result = preg_replace("~(\p{Arabic})~u", "<p class=\"foo\">$1", $str, 1);
echo $result;

The () captures the found arabic character.
The $1 adds it back into the replacement string.

Regex Demo: https://regex101.com/r/yN5vW3/1 vs. https://regex101.com/r/yN5vW3/2
PHP Demo: https://eval.in/537056

chris85
  • 23,846
  • 7
  • 34
  • 51
2

preg_replace() doesn't "cut" the first letter of the string. It replaces it with the string you pass it as its second argument. This is what preg_replace() does for a living; it replaces fragments of strings :-)

There are several ways to tell it what you do. You can, for example, use the sub-string that matches into the replacement() string:

$result = preg_replace("~<p>(\p{Arabic})~u", '<p class="foo">$1', $str, 1);

Or you can use an assertion:

$result = preg_replace("~<p>(?=\p{Arabic})~u", '<p class="foo">', $str, 1);

Anyway, what you probably need is to replace <p> with <p class="foo"> when the paragraph contain Arabic words. The code changes I suggested above already make use of this assumption.

axiac
  • 68,258
  • 9
  • 99
  • 134