如何在PHP中找到其他内容并将其作为字符串返回?

时间:2012-11-08 15:53:21

标签: php url-rewriting preg-replace seo

我想从我的网址

返回这些唯一值
394_black
500_mono2

网址:

/shop/swarovski/394_black_the-reagan-swarovski-maxi-dress
/shop/celeb/500_mono2_the-reagan-swarovski-maxi-dress

我可以在PHP中执行此操作吗?

伪代码

$url    =       get url
$firstvar = after 3rd / and after second _ save text (500_mono2) from $url

$secondvar = using $firstvar return only first 3 numbers
//substr($firstvar, 0, 3);  (I think this will be it)

上面更新了我的问题,更容易理解

1 个答案:

答案 0 :(得分:2)

<?php

    $url = '/shop/celeb/500_mono2_the-reagan-swarovski-maxi-dress';
    $urlparts = explode('/', $url);

    list($var1, $var2, $var3) = explode('_', end($urlparts));

    echo $var1 . '<br>' . $var2 . '<br>' . $var3;

?>