代码之家  ›  专栏  ›  技术社区  ›  user4324916

使用PHP删除<a></a>之间的文本,但不删除href或<span></span>之间的文字

  •  0
  • user4324916  · 技术社区  · 8 年前

    正在尝试从之间删除文本 </span> </a> .我只想删除 some text here 就在 </span> 使得其仅显示图标。

    $bad_string = '<li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>';
    
    $good_string = '<li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>';
    

    目前的反响是:

    <ul id="menu-social-menu-1" class="menu">
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
    </ul>
    

    应该回应什么:

    <ul id="menu-social-menu-1" class="menu">
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
        <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
    </ul>
    

    在WordPress中使用以下代码,跨距后的文本仍然存在。它不会去掉后面的文本 </span> .我错了什么?在这里:

    <?php if ( has_nav_menu( 'social-menu' ) ) {
        $menu = wp_nav_menu( array( 'theme_location' => 'social-menu', 'fallback_cb' => '', 'echo' => false ) );
    
        function convertURL($menu) {
            return preg_replace("/<a target=\"_blank\" href=\"([^\"]+?)\"><span><\/span>.*?<\/a>/", "<a target=\"_blank\" href=\"$1\"><span></span></a>", $menu);
        }
    
        echo $menu;
        }
    ?>
    

    enter image description here

    2 回复  |  直到 8 年前
        1
  •  1
  •   PKeidel    8 年前

    为什么不用正则表达式来解决这个问题呢?

    function convertURL($url) {
      return preg_replace("/<a href=\"([^\"]+?)\">.*/", "<a href=\"$1\"><span></span></a>", $url);
    }
    // input:  <a href="http:www.example.com/dir/handle"><span></span>some text here</a>
    // output: <a href="http:www.example.com/dir/handle"><span></span></a>
    

    编辑:
    test.php:

    <?php
    function convertURL($url) {
      return preg_replace("/<a href=\"([^\"]+?)\">.*/", "<a href=\"$1\"><span></span></a>", $url);
    }
    
    foreach(['<a href="http://linkedin.com/some/vanity"><span></span>some text here</a>', '<a href="http:www.example.com/dir/handle"><span></span>some text here</a>'] as $url) {
      echo "$url => ";
      echo convertURL($url)."\n";
    }
    

    $ php /tmp/test.php
    <a href="http://linkedin.com/some/vanity"><span></span>some text here</a> => <a href="http://linkedin.com/some/vanity"><span></span></a>
    <a href="http:www.example.com/dir/handle"><span></span>some text here</a> => <a href="http:www.example.com/dir/handle"><span></span></a>
    
        2
  •  0
  •   Community M-A    7 年前

    强制性的 https://stackoverflow.com/a/1732454/2191572 但这应该奏效。

    如果我可以重申你的要求,那么你想删除中间的东西 </span> </a>

    $bad_string = '<a href="http:www.example.com/dir/handle"><span>social icon</span>some text here</a>';
    
    $parts = explode('</span>', $bad_string); // explode on </span> and save the array
    array_pop($parts); // remove last element of array which should be "some text here</a>"
    $parts[] = '</a>'; // re-add the </a>
    
    $good_string = implode('</span>', $parts); // re-join the array on </span>
    
    echo $good_string;