【发布时间】:2014-02-26 07:50:24
【问题描述】:
我有一个 HTML 表单,要求用户粘贴一些代码:
<!-- Facebook Badge START -->
<a href="https://www.facebook.com/alex.gk" target="_TOP" style="font-family: "lucida grande",tahoma,verdana,arial,sans-serif; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3B5998; text-decoration: none;" title="Alex Gk">Alex Gk</a>
<span style="font-family: "lucida grande",tahoma,verdana,arial,sans-serif; font-size: 11px; line-height: 16px; font-variant: normal; font-style: normal; font-weight: normal; color: #555555; text-decoration:none;"> | </span>
<a href="https://www.facebook.com/badges/" target="_TOP" style="font-family: "lucida grande",tahoma,verdana,arial,sans-serif; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3B5998; text-decoration: none;" title="Δb7;μιουργήστε το δικό σας διακριτικό!">Δημιουργήστε το δικό σας διακριτικό</a><br/>
<a href="https://www.facebook.com/alex.gk" target="_TOP" title="Alex G">
<img src="https://badge.facebook.com/badge/1565383.3437.1745257502.png" style="border:0px;" />
</a>
<!-- Facebook Badge END -->
我想要在 PHP 中只保留以下部分,以便用户粘贴整个代码 但提交表单后,它只保留以下部分。
<a href="https://www.facebook.com/alex.gk" target="_TOP" title="AlexG">
<img src="https://badge.facebook.com/badge/1383.3437.1745257502.png" style="border:0px;" />
</a>
【问题讨论】:
-
您可以使用explode() 并返回数组,但是如果您知道Alex Gk 中的第一个词或发送词是否为粗体| Δημιουργήστε το δικό σας διακριτικό 你可以像 $str = 'Alex Gk | Δημιουργήστε το δικό σας διακριτικό'; $arr = explode("|",$str);现在 $arr[0] 和 $arr[1] 将包含字符串,如果您知道哪个是粗体,您可以使用该数组索引。
-
我在发布的代码中没有看到任何 bold 部分。如果粗体部分来自您的用户,为什么不发布相关代码。
-
这个粘贴的 html 总是这样构造吗?如果是这样,您可以使用类似 domdocument 加载 html 函数并始终抓住第三个锚点。 php.net/manual/en/domdocument.loadhtml.php