0

我想在起始位置获取字符串中的 html 标记并将其从字符串中删除

我的演示字符串如下:

<I>little willingness</I> that, as the Course itself was to emphasise

这将返回 <I>标记为 o/p

<p>little willingness</p> that, as the Course itself was to emphasise

这将返回 <p>标记为 o/p

little willingness that, <p>as the Course itself was to emphasise</p>

这将返回 null 作为 o/p

如何修改下面的代码以仅检查行首的 HTML 标记,然后将其删除?

preg_match("/<[^<]+>/",$string,$m);
4

2 回答 2

3

第一步:

$paragraph = "<p><i>Please don't</i> blow me to pieces. How to put span here.</p>";
$sentences = explode(".", $paragraph);

接下来,span为每个句子添加标签:

foreach($sentences as &$sentence) {
    $sentence = "<span>$sentence</span>";
}

最后,将它们重新分解成一个段落:

$paragraph = implode(".", $sentences);
于 2013-06-18T11:29:17.397 回答
1

代码

<?php
$str = "<I>little willingness</I> that, <b>as</b> the Course itself was to emphasise again and again.";
if(preg_match('/^(<.*?>)/', $str, $matches))
{
    $str = preg_replace('/^(<.*?>)/', '', $str);
}

print $str;
var_dump($matches);

印刷:

little willingness</I> that, <b>as</b> the Course itself was to emphasise again and again.
array(2) {
  [0]=>
  string(3) "<I>"
  [1]=>
  string(3) "<I>"
}

因此,您将获得开头没有标签的字符串和 $matches 中的标签值。

于 2013-06-18T09:13:05.817 回答