【问题标题】:Ignore if there is URL是否有 URL 忽略
【发布时间】:2013-01-30 15:16:06
【问题描述】:

这个问题是以下问题的延续: Garbage values coming on pulling data from wordpress

我已经使用以下代码处理了垃圾值:

 htmlentities($entry->title, ENT_QUOTES | ENT_IGNORE, 'UTF-8')

上面这段代码的问题是,如果数据中有任何 url,那么它不会显示该 url,而是将 url 破坏为如下所示:

… <a href="http://abc.com/blog/">Continue reading <span class="meta-nav">→</span></a>

如果有 url,请告诉我如何忽略。

【问题讨论】:

  • 网址中的html标签?你在清理输入吗?
  • @Mr.Alien 我正在清理从 wordpress 中提取的数据。这样我就可以忽略像   , – 这样的垃圾值。我上面提到的代码摆脱了垃圾值,但如上所述弄乱了 url
  • 嗨,你可能想看看我的回答:stackoverflow.com/a/14785592/382564
  • @softgenic 刚刚发布了一个答案,试图回答您的用例。它应该工作,让我知道它是怎么回事:)

标签: php


【解决方案1】:

正在使用修复特殊字符数据的 htmlspecialchars_decode() 函数修复 URL 问题。

以下代码行也修复了垃圾值问题以及 URL:

$ret = $feed;     
echo htmlspecialchars_decode(htmlentities($ret, ENT_QUOTES | ENT_IGNORE, 'UTF-8')); 

【讨论】:

    【解决方案2】:

    这是一个 hacky 解决方案,但收集了你是如何处理这个问题的,而不用担心字符编码,你可能只是想让这该死的东西正常工作。

    首先,我们将超链接转换为 hacky BBCode。然后,我们在上面运行htmlentities(),最后我们用旧的HTML 替换hacky A BBCode。看看这个:

    $foo = 'Opening quietly in Chicagos West Loop, the Inspire Business Center is looking to take a more active role in Chicagos startup scene &#8230; Continue reading <span class="meta-nav">&#8594;</span>';
    echo smartencode($foo);
    
    function smartencode($str) {
         $tags = 'a|span';
         // Convert Anchor Tags to hacky-BBCode
         $ret = preg_replace('/\<(\/?('.$tags.').*)\>/U', '[$1]', $str);
    
         // Remove so-called Garbage
         $ret = preg_replace('/[^(\x20-\x7F)]*/','', $ret);
         // $ret = htmlentities($ret, ENT_QUOTES | ENT_IGNORE, 'UTF-8');
    
         // Reinstate Anchor tags in HTML
         $ret = preg_replace('/\[(\/?('.$tags.').*)\]/U', '<$1>', $ret);
         return $ret;
    }
    

    再次,它优雅。事实上,如果您仔细观察,您会发现一些陷阱 - 但我认为它可能只适用于您的用例。

    http://writecodeonline.com/php/ 上测试并按预期工作。

    【讨论】:

    • 谢谢,但它也不起作用,请对以下文本进行测试,您会发现像’这样的垃圾文本没有影响:Opening quietly in Chicago’s West Loop, the Inspire Business Center is looking to take a more active role in Chicago’s startup scene. “As Chicago’s,-
    • @softgenic 我已经修改了代码,请立即测试。它正在丢弃“垃圾”字符。
    • +1 为您的努力,再次感谢,您更新的答案摆脱了垃圾值,但仍然存在链接问题,请查看您函数中的以下转换文本:Opening quietly in Chicagos West Loop, the Inspire Business Center is looking to take a more active role in Chicagos startup scene &amp;#8230; Continue reading &lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt;
    • 应该看起来像 &amp;#8230; Continue reading &lt;span class="meta-nav"&gt;&amp;#8594;&lt;/span&gt; 而不是 … Continue reading →
    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多