如何在标签上引爆一个字符串?

时间:2021-08-01 22:05:05

I have a string like following

我有一个像下面这样的字符串。

Overall Queen Poster <br /> Queen Poster Headboard:<br /> Queen Poster <br /> Queen Footboard  <br /> Queen Poster Rails: 62.28"W x 84.02"D x 18.07"H - 37lbs.

How to explode it on
tag so that I can get the values in an array?

如何在标签上爆炸,这样我就能得到数组中的值?

I tried this

我试着这

   $myString =  "Overall Queen Poster <br /> Queen Poster Headboard:<br /> Queen Poster <br /> Queen Footboard  <br /> Queen Poster Rails: 62.28"W x 84.02"D x 18.07"H - 37lbs";
    $myArray = explode("\n",$myString);
    print_r($myArray);

But not getting correct result.

但是没有得到正确的结果。

2 个解决方案

#1


25  

That's because <br /> != \n.

那是因为
!= \n。

You can use regex to split it, to make sure that you get all the BR tags (<br>, <br />, <br/>, <br class="break">, <BR> etc.)

您可以使用regex对其进行拆分,以确保获得所有的BR标记(< BR>,



等)。

Code:

代码:

<?php
    $myString = 'Overall Queen Poster <br /> Queen Poster Headboard:<br /> Queen Poster <br /> Queen Footboard  <br /> Queen Poster Rails: 62.28"W x 84.02"D x 18.07"H - 37lbs.';

    $myArray = preg_split('/<br[^>]*>/i', $myString);

    print_r($myArray);
?>

Outputs:

输出:

Array
(
    [0] => Overall Queen Poster 
    [1] =>  Queen Poster Headboard:
    [2] =>  Queen Poster 
    [3] =>  Queen Footboard  
    [4] =>  Queen Poster Rails: 62.28"W x 84.02"D x 18.07"H - 37lbs.
)

DEMO

演示

Limitations:

限制:

Will split on any HTML tags that start with br. Meaning that if you made your own tag, such as <breadcrumb>, this would mistakenly be seen as a line break.

将在任何以br开头的HTML标记上拆分。也就是说,如果您创建了自己的标记,比如 ,这将被错误地视为换行符。

Alternatives:

选择:

  • explode('<br />', $myString) if you're sure that your tags will always be exactly <br />
  • 如果您确信您的标记将始终为
    ,那么就会爆炸('
    ', $myString)
  • preg_split('/<br\s*/?>/i', $myString) if you have tags such as <breadcrumb> and don't have any attributes in any <br> tag.
  • preg_split(' / < br \ s * / ?如果您有 <面包屑b> 这样的标签,并且在任何
    标签中都没有任何属性,那么就使用>/i', my$ myString)。

Extra notes:

额外的注释:

If you don't want white-space in your matches, you can change your regex to /<br[^>]*>\s*/i which will also match any white-space characters after the match. Alternatively, you can run it through ltrim with $myArray = array_map('ltrim', $myArray);.

如果你不想要白色空间在你的比赛,你可以改变你的正则表达式/ < br[^ >]* > \ s * /我也将匹配任何空白字符匹配后。或者,您可以使用$myArray = array_map('ltrim', $myArray)在ltrim中运行它;

#2


3  

The input is provided as a html encoded string so the line breaks are encoded as <br /> so why don't you just take <br /> as the separator instead of \n ?

输入以html编码字符串形式提供,因此换行符被编码为
,所以为什么不把
作为分隔符而不是\n呢?

#1


25  

That's because <br /> != \n.

那是因为
!= \n。

You can use regex to split it, to make sure that you get all the BR tags (<br>, <br />, <br/>, <br class="break">, <BR> etc.)

您可以使用regex对其进行拆分,以确保获得所有的BR标记(< BR>,



等)。

Code:

代码:

<?php
    $myString = 'Overall Queen Poster <br /> Queen Poster Headboard:<br /> Queen Poster <br /> Queen Footboard  <br /> Queen Poster Rails: 62.28"W x 84.02"D x 18.07"H - 37lbs.';

    $myArray = preg_split('/<br[^>]*>/i', $myString);

    print_r($myArray);
?>

Outputs:

输出:

Array
(
    [0] => Overall Queen Poster 
    [1] =>  Queen Poster Headboard:
    [2] =>  Queen Poster 
    [3] =>  Queen Footboard  
    [4] =>  Queen Poster Rails: 62.28"W x 84.02"D x 18.07"H - 37lbs.
)

DEMO

演示

Limitations:

限制:

Will split on any HTML tags that start with br. Meaning that if you made your own tag, such as <breadcrumb>, this would mistakenly be seen as a line break.

将在任何以br开头的HTML标记上拆分。也就是说,如果您创建了自己的标记,比如 ,这将被错误地视为换行符。

Alternatives:

选择:

  • explode('<br />', $myString) if you're sure that your tags will always be exactly <br />
  • 如果您确信您的标记将始终为
    ,那么就会爆炸('
    ', $myString)
  • preg_split('/<br\s*/?>/i', $myString) if you have tags such as <breadcrumb> and don't have any attributes in any <br> tag.
  • preg_split(' / < br \ s * / ?如果您有 <面包屑b> 这样的标签,并且在任何
    标签中都没有任何属性,那么就使用>/i', my$ myString)。

Extra notes:

额外的注释:

If you don't want white-space in your matches, you can change your regex to /<br[^>]*>\s*/i which will also match any white-space characters after the match. Alternatively, you can run it through ltrim with $myArray = array_map('ltrim', $myArray);.

如果你不想要白色空间在你的比赛,你可以改变你的正则表达式/ < br[^ >]* > \ s * /我也将匹配任何空白字符匹配后。或者,您可以使用$myArray = array_map('ltrim', $myArray)在ltrim中运行它;

#2


3  

The input is provided as a html encoded string so the line breaks are encoded as <br /> so why don't you just take <br /> as the separator instead of \n ?

输入以html编码字符串形式提供,因此换行符被编码为
,所以为什么不把
作为分隔符而不是\n呢?