duanlian1960 2011-04-07 17:41
浏览 82
已采纳

PHP preg_match标签+花括号

I have a string such as:

#sometag-{serialized-data-here}

And I want to match this pattern, but use everything inside the curly braces (so I can unserialize it later). How can I match this text pattern with preg_match()?

So far I have:

preg_match('~{[^{}]*}~', $text, $match);

but this just matches the contents of the braces if in $text without the hash tag.

EDIT: Here is the logic of what im trying to accomplish:

  $user_post = "Here is my cool post that contains some media.";
  $media = array("mediatype" => "sometype", "id" => "ebJ2brErERQ", "title" => "Some cool video", "description" => "Some cool description");  
  $user_post .= "#sometag-" . serialize($media);

Later, when I fetch $user_post from the database, I want to match the text, strip it out and display the media.

I'll have something like this:

Here is my cool post that contains some media.#sometag-a:4:{s:9:"mediatype";s:8:"sometype";s:2:"id";s:11:"ebJ2brErERQ";s:5:"title";s:15:"Some cool video";s:11:"description";s:21:"Some cool description";}
  • 写回答

3条回答 默认 最新

  • douxiajiao8445 2011-04-07 17:48
    关注

    Why not use explode()?

    $tag_data_arr = explode('-', $text, 2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探