473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

preg_replace and back reference

I wrote the following code and it does NOT seem to work.

$search = array('/<popup[^>]*>/ie');
$replace = array('make_popup(split_tag(\\0))');
preg_replace($search, $replace, $someText);

If I try the following code, then the replacing seems to work (I just try to get a
dummy back reference \\1)

$search = array('/<p(op)up[^>]*>/ie');
$replace = array('make_popup(split_tag(\\1))');
preg_replace($search, $replace, $someText);

What is wrong in the first part? Thanks and cheers,

richard
Jul 17 '05 #1
1 3672
Richard B. Christ wrote:
I wrote the following code and it does NOT seem to work.

$search = array('/<popup[^>]*>/ie');
$replace = array('make_popup(split_tag(\\0))');
preg_replace($search, $replace, $someText);

If I try the following code, then the replacing seems to work (I just try to get a
dummy back reference \\1)

$search = array('/<p(op)up[^>]*>/ie');
$replace = array('make_popup(split_tag(\\1))');
preg_replace($search, $replace, $someText);

What is wrong in the first part? Thanks and cheers,


Why do you need the /e modifier to preg_replace()?

The first snippet will try to evaluate
make_popup(split_tag(<popup parm1="value1" parm2="value2">))
as if it was php code.

Maybe you want to evaluate
make_popup(split_tag('<popup parm1="value1" parm2="value2">'))
// __________________^_______________________________ ______^__
this instead.
The second snippet (which is also wrong!!) will try to evaluate
make_popup(split_tag(op))

which php will convert (with a warning) to
make_popup(split_tag('op'))

So ... turn on all warnings:
<?php error_reporting(E_ALL); ?>

And check the result of preg_replace without the /e modifier first.

Happy Coding :-)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Randell D. | last post by:
Folks, I have a string that *could* contain any character - I want it to call a function that will allow me to pass the variable, and then return it cleaned of anything except alphanumeric,...
1
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the...
7
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore...
3
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
16
by: bissatch | last post by:
Hi, I am trying to use preg_replace to take out all occurances of PHP code after reading (fread()) the contents of a PHP file. The code I am using is: $html = preg_replace("<?php*?>", "",...
2
by: yawnmoth | last post by:
Can anyone help me understand the following scripts output?: <? echo preg_replace('/.*/e',"sprintf('\\\\\')",''); ?> The output is \ and this doesn't make a lot of sense to me. Since \\'s...
1
by: lawrence k | last post by:
Want to replace the limit clause in a query, but can't get it right. What's wrong with this: $pattern = "(.*)limit (.*)"; $replacement = '$1'; $replacement .= "LIMIT $limit"; $replacement .=...
2
by: Gilles Ganault | last post by:
Hello I went through some examples, tried a bunch of things... but still can't figure out why I can't extract the TITLE section of a web page using preg_replace(): ----------- <?php $url...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.