473,230 Members | 1,535 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,230 software developers and data experts.

Help with preg_replace

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

---
Sig line1
Sig line2
Sig line3

If I could just get rid of that, it would be pretty good. But I also get this
kind of junk a lot, since messages are being quoted:
---
Sig line1
Sig line2
Sig line3


or
---
Sig line1
Sig line2
Sig line3


so I thought I'd be smart and tried:

$body = preg_replace("/---.*?Sig line1.*?Sig line2.*?Sig line3/","",$body);

but this erased the entire message somehow. So I thought I'd go back to
basics and tried:

$body = preg_replace("---","",$body);
$body = preg_replace("Sig line1","",$body);
$body = preg_replace("Sig line2","",$body);
$body = preg_replace("Sig line3","",$body);

but this erased everything too.

I'm kinda stumped. Why are these erasing the entire message? And what's the
actual smart way to erase this signature when it can have any amount of white
space and >'s between lines?

Thanks for any help.
Jul 17 '05 #1
3 5288
Regarding the first regex you need to set the multiline flag
to 's', although to be on the safe side you can add 'i' in order to
ignore case.
Therefore you're php code should end up like so:

$body = preg_replace("/---.*?Sig line1.*?Sig line2.*?Sig
line3/si","",$body);

More clearly:
( "/<regex here>/si", "", $body );

To remove quotes you should put something like:
$body = preg_replace('/>.*/', '', $body);
without '//si' !

In summary 'si' comes in very handy at times.

Jul 17 '05 #2
On 6/20/2005 1:08:49 AM, "merlinfly" wrote:
Regarding the first regex you need to set the multiline flag
to 's', although to be on the safe side you can add 'i' in order to
ignore case.
Therefore you're php code should end up like so:

$body = preg_replace("/---.*?Sig line1.*?Sig line2.*?Sig
line3/si","",$body);

More clearly:
( "/<regex here>/si", "", $body );

To remove quotes you should put something like:
$body = preg_replace('/>.*/', '', $body);
without '//si' !

In summary 'si' comes in very handy at times.


Any idea why my syntax blew away the whole text though? Sounds like I should
be getting fewer matches if it wouldn't go across lines.
Jul 17 '05 #3
Regarding:
$body = preg_replace("/---.*?Sig line1.*?Sig line2.*?Sig
line3/","",$body);
I
t didn't blow away the lines; when preg_replace() does not find a match
it returns an emtpy string, ''. That's just how preg_replace() works.
without the //si you would be telling it to search single lines which
contain the dashes and Sig line x separated by anything apart from a
new line.

You could take it as a signal to perfect your regular expression from
that point on.

Jul 17 '05 #4

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

Similar topics

3
by: JDJones | last post by:
I'm having some trouble getting this replacement to work efficiently. I have the following code: $thing = preg_replace("(\r\n|\n|\r|\t)", "", $thing1); What I want to do now is to get it to...
9
by: jn | last post by:
I'm stripping out the attributes in <TD> tags...but I want to strip out everything BUT the COLSPAN attribute. The following strips out all attributes. What do I do if I want to keep a certain...
4
by: Sidharta | last post by:
Hi all, how come this doesn't work????? # convert to unix new lines $text = preg_replace("/\r\n/", "\n", $text); # remove extra new lines $text = preg_replace("/\n+/", "\n", $text); is...
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...
2
by: Fotozine | last post by:
well, I have this peace of code which isn't woking as I would like it to work. $tekst = preg_replace('/\\\"/', '&quot;', $tekst); In my forms, I would like to change " to &quot; but this line is actualy...
1
by: K. | last post by:
Hello! I would like to use preg_replace function to leave in the result variable only: 1) letters from A to Z including Polish letters like including Polish upper letters like ...
4
by: William Gill | last post by:
I am not well versed on regular expressions, but have fumbled through enough to come up with these three preg_replace() statements (I'm not even sure how the third one works, but it does)to reduce...
1
by: minimatrix | last post by:
Hi, I am quite new to php but picking it up quite quickly, however I dont understand how a preg replace works. I am trying to count the number of columns selected but when I do the count I sometimes...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.