douchi7073 2014-08-13 22:30
浏览 24
已采纳

来自字符串的php regex属性

I've got a string like this:

family="ABeeZee;100regular" decoration="" style="normal" txtalign="txt-left" size="14px" line="22px" padding="0px" bold="" uppercase="" color=""" 

And I want just to get the value/string inside family:

 ABeeZee;100regular

I know that I need to use regex but I still don't understand how to write the pattern...

I tried this without success:

$mystring = 'family="ABeeZee;100regular" decoration="" style="normal" txtalign="txt-left" size="14px" line="22px" padding="0px" bold="" uppercase="" color=""" '
$pattern  = '/(family)="([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)"/is';
$string   = preg_replace($pattern, "", $mystring);
  • 写回答

1条回答 默认 最新

  • duanbodai5166 2014-08-13 22:35
    关注

    Use preg_match() instead of preg_replace() to grab the value.

    You only have a single pattern so you don't need to use a conditional regex here.

    preg_match('/family="([^"]+)"/', $mystring, $match);
    echo $match[1]; //=> "ABeeZee;100regular"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面