PHP正则表达式替换为url

时间:2017-01-13 09:43:21

标签: php regex

我需要帮助来替换我的url中最后一次出现的数字uri字符串: 例如,我有url字符串:

@foreach($vehicles as $vehicles)
        <div class="col-sm-4">
            <!-- Product -->
                <div class="shop-item">
                    <!-- Product Image -->
                            <div class="image">
                                <a href="page-product-details.html"><img src="{{ asset("images/$vehicles->$image->title") }}" alt="{{ $vehicles->$image->title }}" ></a>
                            </div>
                    <!-- Product Title -->
                            <div class="title">
                                <h2><a href="page-product-details.html">{{$vehicles->vname}}</a></h2>
                                <h3><a href="page-product-details.html">{{$vehicles->lotno}}</a></h3>
                           </div>
@endforeach

我想删除最后一次出现的“/ 5”或“/ 21”或者上一个带有空字符串的正斜杠之后的任何其他数字并且仅获取:

$url1 = "http://localhost/mystore/electronics/company/5-mobile/5";
$url2 = "http://localhost/mystore/electronics/company/5-mobile/21";
$url3 = "http://localhost/mystore/electronics/company/5-mobile";

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

不使用preg替换,只是一种简单的方法:

<td *ngFor="let month of months | keyObject">{{month.value}}</td>

参考:

http://php.net/manual/en/function.dirname.php

http://php.net/manual/en/function.explode.php