[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

preg replace

Posted on 2011-05-03
8
Medium Priority
?
372 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:jccyber
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35623779
$string = preg_replace("#(href=\")/#","$1http://domain.com/",$string);
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35643247
You can simply use the following:
$str = "href=\"/";
$str = preg_replace("/href=\"\//", "http://domain.com/", $str);

Open in new window

0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35644835
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35690496
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35694229
> 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
 
LVL 35

Accepted Solution

by:
Terry Woods earned 1000 total points
ID: 35694238
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
 

Author Closing Comment

by:jccyber
ID: 35694253
Thank you, TerryAtOpus That is what I was looking for. I need to learn more about preg replace.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35694275
@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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question