preg replace

Hello Experts,

I am trying to use preg_replace in this scenario

I am trying to replace this: href="/ with this href="http://domain.com/

How?
jccyberAsked:
Who is Participating?
 
Terry WoodsConnect With a Mentor IT GuruCommented:
There are other ways in which a regex would be necessary too, such as making sure the href is within an <a> tag (in which case there may be other attributes such as class)
0
 
Terry WoodsIT GuruCommented:
$string = preg_replace("#(href=\")/#","$1http://domain.com/",$string);
0
 
Mohamed AbowardaSoftware EngineerCommented:
You can simply use the following:
$str = "href=\"/";
$str = preg_replace("/href=\"\//", "http://domain.com/", $str);

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Mohamed AbowardaSoftware EngineerCommented:
Please ignore my above post and use the following code instead:
$str = "gwag href=\"/";
$str = preg_replace("/href=\"\//", "href=\"http://domain.com/", $str);

Open in new window

0
 
Ray PaseurCommented:
Looking at the example posted here I would say that you do not need a regular expression at all.  Something like this code snippet would be simpler and faster.  If there is more to this question that we are not seeing, please let us know.


<?php

$str = '<a href="/images/picture.jpg">pic</a>';

$old = 'href="/';
$rep = 'href="http://domain.com/';
$new = str_replace($old, $rep, $str);

Open in new window

0
 
Terry WoodsIT GuruCommented:
> Looking at the example posted here I would say that you do not need a regular expression at all.

Agreed, for the description of the problem given. However, if it is desirable to relax the pattern we're trying to match, then using a regular expression allows us to have an unknown number of space characters between "href" and "=" etc. Eg:
$string = preg_replace("#(href\s*=\s*\")/#","$1http://domain.com/",$string);
0
 
jccyberAuthor Commented:
Thank you, TerryAtOpus That is what I was looking for. I need to learn more about preg replace.
0
 
Ray PaseurCommented:
@jccyber, going forward, if you post a couple of examples showing the variations on the input theme and the expected outputs, you may get better answers.  For better or worse, computer programming is a fairly literal craft and problem definitions are more than half the battle.  There is certainly nothing wrong with using REGEX and we might even be able to give you more constructive advice about the design of the application if we had a little more visibility into what you are getting as an input and what you want as an output.

Best to all, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.