【问题标题】:PHP explode string with tags using UTF8 between themPHP 爆炸字符串与标签之间使用 UTF8
【发布时间】:2017-09-18 14:53:09
【问题描述】:

在 php 中,我想在它们之间使用 utf-8 来分解带有标签的字符串,例如,在此文本中:

$content = "<heading>فهرست اول</heading>hi my name is mahdi  whats app <heading>فهرست دوم</heading>how are you";

因为我必须&lt;heading&gt;&lt;/heading&gt; 标签与它们之间的 utf8,我想与它们有一个简单的数组,例如:

$arr[0] = "<heading>فهرست اول</heading>hi my name is mahdi  whats app";
$arr[1] = "<heading>فهرست دوم</heading>how are you";

&lt;heading&gt;&lt;/heading&gt; 之间的字符串不同,我该如何制作这个数组?问题是我如何通过&lt;heading&gt;ENY TEXT&lt;/heading&gt; 分解文本

【问题讨论】:

  • 你试过使用正则表达式吗? preg_split/(?=&lt;heading&gt;.*?&lt;\/heading&gt;)/ 作为模式应该可以工作......
  • 它应该回答你的问题:stackoverflow.com/questions/5696412/…
  • @Soaku 不,你能帮我如何使用这个要求吗?
  • @Mahdi.Pishguy $arr = preg_split('/(?=&lt;heading&gt;.*?&lt;\/heading&gt;)/', $content) 将拆分 &lt;heading&gt; 标签上的字符串,无论其内容如何,​​都不会删除它。这应该工作......
  • @Soaku 是的,工作正常,但我想在与父母之间有标签,我不想删除heading

标签: php


【解决方案1】:

您可以使用preg_split通过正则表达式拆分文本,然后使用array_filter删除空字符串:

$arr = array_filter(preg_split('/(?=<heading>.*?<\/heading>)/', $contents), 'strlen');

它不会删除标签,因为它位于 look-ahead 中 - 一个不消耗匹配内容的组构造。

例如:

<heading>فهرست اول</heading>hi my name is mahdi  whats app <heading>فهرست دوم</heading>how are you

这应该返回:

array(
  [0] => "<heading>فهرست اول</heading>hi my name is mahdi  whats app ",
  [1] => "<heading>فهرست دوم</heading>how are you"
)

您可以在线查看此正则表达式:https://regex101.com/r/ITi7Lh/1
或者,如果您愿意,请查看 PHP 是如何解析它的:(该链接似乎不适用于 SO,您必须手动粘贴它):https://en.functions-online.com/preg_split.html?command={"pattern":"\/(?=&lt;heading&gt;.*?&lt;\\\/heading&gt;)\/","subject":"&lt;heading&gt;\u0641\u0647\u0631\u0633\u062a \u0627\u0648\u0644&lt;\/heading&gt;hi my name is mahdi whats app &lt;heading&gt;\u0641\u0647\u0631\u0633\u062a \u062f\u0648\u0645&lt;\/heading&gt;how are you","limit":-1}

【讨论】:

  • 对不起先生,我有这个结果给你代码:Array ( [1] =&gt; فهرست اولhi my name is mahdi whats app [2] =&gt; فهرست دومhow are you )
  • 对不起先生,您说得对,看到源页面后我知道结果是正确的
  • @Mahdi.Pishguy 我正要说这个,但不知道为什么我没有发布那个。很高兴知道它有帮助。 :)
  • @Mahdi.Pishguy 如果您不使用任何其他标签,strip_tags 将删除 html 符号而不删除内容。否则,您可以使用一些正则表达式... `//
  • @Mahdi.Pishguy 我很乐意提供帮助 :) 只是想指出,即使正则表达式可能很简单,但它们并不总是最好的解决方案。如果您使用复杂的正则表达式或过于频繁,您可能会注意到速度变慢。如果存在替代方案 - 使用它。这里的其他人已经提供了一些,所以他们可能对你更好。
【解决方案2】:

如果您的 UTF 引起问题,您可以使用 strpos 和 Substr 来做同样的事情。

这将循环直到找不到更多标题,然后在循环后添加最后一个 Substr。

https://3v4l.org/UPfbb

$content = "<heading>فهرست اول</heading>hi my name is mahdi  whats app <heading>فهرست دوم</heading>how are you<heading>فهرست اول</heading>hi my name is mahdi  whats app2 <heading>فهرست دوم</heading>how are you2";

$oldpos =0;
$pos =strpos($content, "<heading>",1); // offset 1 to exclude first heading.

While($pos !== false){
    $arr[] = Substr($content, $oldpos, $pos-$oldpos);
    $oldpos = $pos;
    $pos =strpos($content, "<heading>",$oldpos+1); //offset previous position + 1 to make sure it does not catch the same again 
}
$arr[] = Substr($content, $oldpos); // add last one since it does not have a heading tag after itself.
Var_dump($arr);

【讨论】:

    【解决方案3】:

    您可以使用preg_match,或者在您的情况下使用preg_match_all

    $content = "<heading>فهرست اول</heading>hi my name is mahdi  whats app <heading>فهرست دوم</heading>how are you";
    
    preg_match_all("'<heading>.*?<\/heading>'si", $content, $matches);
    print_r($matches[0]);
    

    给予:

    Array
    (
        [0] => <heading>فهرست اول</heading>
        [1] => <heading>فهرست دوم</heading>
    )
    

    【讨论】:

    • 他希望在下一个标签之前也能得到标签后面的内容。不仅是里面的东西
    【解决方案4】:

    您可以尝试以下功能,它应该可以很好地满足您的需求。基本上你应该使用&lt;heading&gt;作为分隔符分割数组,结果数组中的每个项目都是你需要的,但是标题标签将被剥离,因为它是你分割的,所以你需要添加它背部。有 cmets 解释代码在做什么。

    function get_what_mahdi_wants($in_string){
    
      $mahdis_strings_array = array();
    
      // Split string at occurrences of '<heading>'
      $mahdis_strings = explode('<heading>', $in_string);
      foreach($mahdis_strings as $mahdis_string){
    
        // if '<heading>' is found at start of string, empty array element will be created. Skip it.
        if($mahdis_string == ''){ continue; }
    
        // Add back string element with '<heading>' tag prepended since exploding on it stripped it.
        $mahdis_strings_array[] = '<heading>'.$mahdis_string;
      }
      return $mahdis_strings_array;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 2011-12-10
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多