用php编辑数组值

时间:2012-07-09 18:00:57

标签: php str-replace

我有这样的输出:

Array
(
    [0] => stdClass Object
        (
            [post] => john Has site <a href='http://www.mysite.com/events/info/4240'>this is a test</a>
            [date_created] => 1341853220
        )

    [1] => stdClass Object
        (
            [post] => jane Has site <a href='http://www.mysite.com/events/info/1'>test venue</a>
            [date_created] => 1341849999
        )

    [2] => stdClass Object
        (
            [post] => james Has site <a href='http://www.mysite.com/events/info/4240'>this is a test</a>
            [date_created] => 1341680695
        )

我想知道是否有办法得到这样的结果:

Array
(
    [0] => stdClass Object
        (
            [post] => john Has site this is a test
            [number] => 4240
            [date_created] => 1341853220
        )

    [1] => stdClass Object
        (
            [post] => jane Has site test venue
            [number] => 1
            [date_created] => 1341849999
        )

    [2] => stdClass Object
        (
            [post] => james Has site this is a test
            [number] => 4240
            [date_created] => 1341680695
        )

我想要的是消除html标签和url,只需将名称和数字保留在url的末尾,以便我可以存储它并在以后使用它。 我试图在foreach中使用str_replace但是我找不到正确的方法来实现它还是有另一种方法来实现它吗?

感谢

2 个答案:

答案 0 :(得分:2)

  1. 使用RegEx匹配href:<a href='([^\']+?)'>

  2. explode()匹配的字符串与分隔符“/”。

  3. 获取最后一个元素number

  4. 使用条带化标记post和新元素number构建一个新的对象数组。

答案 1 :(得分:0)

你应该像Shubham所说的那样使用正则表达式。

使用PHP的preg_match和以下模式,可以从帖子中检索所需的数据。然后可以创建具有所需结果的新阵列。

#(.+)<a[^>]+?([0-9]+)'>(.+)</a>#

相关问题