tags:

views:

65

answers:

2

Hello!

I'm trying to transform this:

http://link_brochure/1/2 OR link_brochure/1/2

into this:

class="show-brochure" href="http://localhost/main/brochure/1/2"

But I'm a rookie when it comes about regular expressions... Any ideas?

Thanks!

A: 

I'd just use str_replace to replace 'main/' with 'link_', then 'a ' with 'a class="show-brochure"'.

On iPad, so can't be bothered to type code, but look up str_replace, and you'll get the idea!

Rich Bradshaw
Hi, Rich! I asked the wrong question initially.But thanks for your answer :)
cili
+2  A: 

Something like this?

$str=preg_replace(
    '{(?:http://)?link_brochure/(\d+)/(\d+)}', 
    'class="show-brochure" href="http://localhost/main/brochure/$1/$2"', 
    $str);

Edit: updated following question edit (Thanks Peter).

Paul Dixon
Thank you, Paul!
cili
Question has changed slightly - need to add `(?:http://)?` to make that part optional, and add `localhost/` to the replacement side.Probably also worth explaining that the `$1` and `$2` refer to the two captured `(\d+)` bits, so the link will work regardless of what numbers are there.
Peter Boughton
Great! Thanks, Peter!
cili