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

x
?
Solved

PHP preg_replace help

Posted on 2007-07-24
5
Medium Priority
?
190 Views
Last Modified: 2013-12-12
Back again....need to fix the output of this example using preg_replace:

href="$http_site_root/companies/one.php?company_id=1643">

to this:

href="xrms/companies/one.php?company_id=1643" target="_blank">

want to add the target to open a new window.

0
Comment
Question by:pdheady
  • 4
5 Comments
 
LVL 1

Author Comment

by:pdheady
ID: 19560366
i tried something like this but didnt work.

echo preg_replace('#company_id="(.*)"#','company_id="$1" target="blank"', $data);
0
 
LVL 1

Author Comment

by:pdheady
ID: 19560376
Well it's more like this.

$data2 .= preg_replace('#\$http_site_root#', '/xrms', $data);

$data3 = preg_replace('#company_id="(.*)"#','company_id="$1" target="blank"', $data2);

echo $data3;
0
 
LVL 13

Accepted Solution

by:
MasonWolf earned 2000 total points
ID: 19561602
$data = str_replace(array('$http_site_root','>'),array( '/xrms',' target="_blank">'), $data);

I find that str_replace is much faster than preg_replace, and so it's worth using whenever possible.
0
 
LVL 1

Author Comment

by:pdheady
ID: 19561685
that did not work.
0
 
LVL 1

Author Comment

by:pdheady
ID: 19561698
my bad, i didnt output it correctly. works like a charm now! thanks.

echo str_replace(array('$http_site_root','>'),array( '/xrms',' target="_blank">'), $data);

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 21 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