mirza
mirza

Reputation: 5793

How to change last segment of url with php preg_replace?

I have urls like this:

/img/products/loop/blabla/81.jpg
/img/products/loop/blabla/asasd/811234.jpg
/img/productsasdasd.jpg

I want to replace this url's like this:

/img/products/loop/blabla/null.jpg
/img/products/loop/blabla/asasd/null.jpg
/img/null.jpg

How to done this ?

Upvotes: 1

Views: 726

Answers (2)

ramblex
ramblex

Reputation: 3057

Here's one way of doing it although you may also want to look at parse_url

<?php

$urls = array(
  '/img/products/loop/blabla/81.jpg',
  '/img/products/loop/blabla/asasd/811234.jpg',
  '/img/productsasdasd.jpg'
);

foreach ($urls as $url) {
  var_dump(preg_replace('#/\w+(\.\w+)$#', '/null$1', $url));
}

Upvotes: 0

Man Programmer
Man Programmer

Reputation: 5356

    $string="/img/products/loop/blabla/81.jpg";
   echo preg_replace("/[A-Za-z0-9]+[.]/","null.",$string);

Upvotes: 1

Related Questions