douxitao8170 2014-02-23 22:56
浏览 123
已采纳

使用preg_split()使用<span>拆分字符串

I have a sting that is in this format.

<span class="amount">$25</span>–<span class="amount">$100</span>

What I need to do is split that into two strings. The string will remain in the same format but the prices will change. I tried using str_split() but because the price changes I wouldn't be able to always know how many characters to split the string at.

What I am trying to get is something like this.

String 1

<span class="amount">$25</span>–

String 2

<span class="amount">$100</span>

It seems the best option I have found is to use preg_split() but I don't know anything about regex so I'm not sure how to format the expression. There may also be a better way to handle this and I just don't know of it.

Could someone please help me format the regex, or let me know of a better way to split that string.

Edit

Thanks to @rm-vanda for helping me figure out that I don't need to use preg_split for this. I was able to split the string using explode(). The issue I was having was because the '-' was encoded weird and therefore not returning correctly.

  • 写回答

2条回答 默认 最新

  • dongtang8678 2014-02-23 22:59
    关注

    If it always has the "-" then this would be the most simple way:

    $span = explode("-", $spans); 
    
    echo $span[0]; 
    
    echo $span[1]; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数
  • ¥15 关于#线性回归#的问题:【统计】回归系数要转化为相关系数才能进行Fisher' Z转化吗(相关搜索:回归模型)
  • ¥100 使用matlab解决含分段变量的优化问题
  • ¥15 matlab基于鲸鱼算法优化vmd
  • ¥20 PCB设计与制作问答
  • ¥20 课堂跑如何下载ppt
  • ¥15 STM32阿里云平台显示不了传感器采集的数据
  • ¥20 三菱FX系列PLC上位机串口下指令置位M64和M65,这两条指令分别是什么呢?
  • ¥15 有关结冰模拟程序咨询