使用php编辑数组值


edit array value with php

我有这样的输出:

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,但我找不到正确的方法,或者有其他方法可以做到这一点吗?

感谢

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

  2. explode()—具有分隔符"/"的匹配字符串。

  3. 获取最后一个元素number

  4. 构造具有剥离标签post和新元素number的对象的新阵列。

正如Shubham所提到的,您应该使用正则表达式。

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

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