【问题标题】:How to use php to strip text except specific elements如何使用php去除特定元素以外的文本
【发布时间】:2016-10-10 17:59:45
【问题描述】:

我正在寻找最好的方法,而不是 JavaScript,在 PHP 中,从 <a> 元素中去除所有其他文本或标记,<spans> 除外。父 <a> 元素不提供要定位的类名或 id。例如:

我有这个 PHP:

<?php if ( has_nav_menu( 'social-menu' ) ) { ?>
  <?php wp_nav_menu( array( 'theme_location' => 'social-menu', 'fallback_cb' => '' ) );?>
<?php}?>

生成这个 html:

<div>
  <ul>
    <li><a><span>icontext</span> some more text to hide1!</a></li>
    <li><a><span>icontext</span> some more text to hide1!</a></li>
    <li><a><span>icontext</span> some more text to hide1!</a></li>
  </ul>
</div>

我希望最终结果是:

<div>
  <ul>
    <li><a><span>icontext</span></a></li>
    <li><a><span>icontext</span></a></li>
    <li><a><span>icontext</span></a></li>
  </ul>
</div>

我理解逻辑将类似于以下使用正确的剥离语法:

if this = '<span>icontext</span>somemoretexttohide1!'
else if this = '<span>icontext</span> some more text to hide1!'
should just = '<span>icontext</span>'

【问题讨论】:

  • 你试过 strip_tags()
  • 我了解它的作用并且一直在 PHP.net 中阅读。 strip_tags("Hello &lt;b&gt;&lt;i&gt;world!&lt;/i&gt;&lt;/b&gt;","&lt;b&gt;") 我知道这会留下&lt;b&gt; 标签但删除&lt;i&gt;。但是我不明白如何检查以找到&lt;a&gt;,然后留下&lt;spans&gt; 和它的文本,但删除span 之外的所有其他内部文本和字符。特别是因为我没有剥离标签。只是父标签的内容,但不是太多,以至于它也剥离了跨度的内容。然后如何将所有这些逻辑添加到 Wordpress 数组中。

标签: php html trim strip


【解决方案1】:
$text = "
  <div>
    <ul>
      <li><a><span>icontext</span> some more text to hide1!</a></li>
      <li><a><span>icontext</span> some more text to hide1!</a></li>
      <li><a><span>icontext</span> some more text to hide1!</a></li>
    </ul>
  </div>
  ";

$start = 0;
while ( strpos( $text, "</span>", $start ) <> FALSE ) {
  $start = strpos( $text, "</span>", $start );
  $length = strpos( $text, "</a>", $start ) - $start;
  $remove = substr( $text, $start, $length );
  $text = str_replace( $remove, "", $text );
  ++$start;
}

【讨论】:

  • 谢谢本...这是我的大脑的跳跃。我意识到我需要首先以某种方式将 wp_nav_menu 放入 $text。然后立即生成它以在实际打印到屏幕之前对其进行搜索。 $text = wp_nav_menu( array( 'theme_location' =&gt; 'social-menu', 'fallback_cb' =&gt; '', 'echo' =&gt; false ) );
【解决方案2】:

根据@ben-shoval 的指示,最终结果是这样的。效果很好!如果有人对性能改进或更清洁的方式有任何进一步的建议,请提出并投票。

<?php if ( has_nav_menu( 'social-menu' ) ) {

    // get the actual output of the html, but don't print it just yet.
    $menu = wp_nav_menu( array( 'theme_location' => 'social-menu', 'fallback_cb' => '', 'echo' => false ) );

    // start removing all text except what's inside the spans.
    $start = 0;
        while ( strpos( $menu, "</span>", $start ) <> FALSE ) {
        $start = strpos( $menu, "</span>", $start )+7;
        $length = strpos( $menu, "</a>", $start ) - $start;
        $remove = substr( $menu, $start, $length );
        $menu = str_replace( $remove, "", $menu );
        ++$start;
    }

    // now that it's modified let this HTML print to screen.
    echo $menu;
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多