Gerald Ferreira
Gerald Ferreira

Reputation: 1337

Escaping "." in preg_replace

What am I doing wrong?

<?php 
    $imageurl = $pagename1; 
    $imageurl = preg_replace('/.asp/', .$g_sites_img2.'.jpg', $pagename1);
?>

I am trying to escape the . in the preg_replace.

I have also tried:

<?php
    $imageurl = $pagename1;
    $imageurl = preg_replace('/\.asp/', .$g_sites_img2.'\.jpg', $pagename1);
?>

Why is it still giving me an error?

Upvotes: 1

Views: 228

Answers (3)

sholsinger
sholsinger

Reputation: 3078

Try this:

<?php $imageurl = preg_replace('/\.asp/', $g_sites_img2.'\.jpg', $pagename1);?>

Notice the missing leading . in the 2nd argument of the preg_replace() call. Also, there is no need for the first line since you're writing the result of preg_replace to that variable anyway.

Upvotes: 1

dtbarne
dtbarne

Reputation: 8200

It doesn't look like preg_replace is necessary.

Why can't you just use str_replace? Anyway, you've got a syntax error (an extra period).

$imageurl = preg_replace('/\.asp/', $g_sties_img2 . '.jpg', $pagename1);

Upvotes: 4

Rafe Kettler
Rafe Kettler

Reputation: 76955

You have an extra . before $g_sites_img2.

$imageurl = preg_replace('/\.asp/', .$g_sites_img2.'\.jpg', $pagename1);?>
                                    ^ Here's your problem

I concur with @dtbarne -- preg_replace() is totally unnecessary here. You should be using str_replace() instead.

Upvotes: 9

Related Questions