doudi8829 2016-03-15 18:07
浏览 62

preg_match字符串中的所有段落

The following string contains multiple <p> tags. I want to match the contents of each of the <p> with a pattern, and if it matches, I want to add a css class to that specific paragraph.

For example in the following string, only the second paragraph content matches, so i want to add a class to that paragraph only.

$string = '<p>para 1</p><p>نص عربي أو فارسي</p><p>para3</p>';

With the following code, I can match all of the string, but I am unable to figure out how to find the specific paragraph.

$rtl_chars_pattern = '/[\x{0590}-\x{05ff}\x{0600}-\x{06ff}]/u';
$return = preg_match($rtl_chars_pattern, $string);
  • 写回答

2条回答 默认 最新

  • dousi4900 2016-03-15 18:12
    关注

    https://regex101.com/r/nE5pT1/1

    $str = "<p>para 1</p><p>نص عربي أو فارسي</p><p>para3</p>"; 
    $result = preg_replace("/(<p>)[\\x{0590}-\\x{05ff}\\x{0600}-\\x{06ff}]/u", "<p class=\"foo\">", $str, 1);
    
    评论

报告相同问题?