在字符串的开头标识HTML标记并将其删除

时间:2013-06-18 09:07:04

标签: php regex

我希望在起始位置的字符串中获取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);

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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中的标记值。