473,799 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("S ig line1","",$body );
$body = preg_replace("S ig line2","",$body );
$body = preg_replace("S ig 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 5326
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
5322
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 remove any semi-colons in the string as well as replace the new line, carriage return and tab delimiters. I can't seem to find a way to make the one preg_replace expression do both. I've had to rely on a second independent preg_replace statement...
9
3246
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 one? eregi_replace("<TD*>","<TD>", $string); I suck at regular expressions. I need some help.
4
10160
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 there better ways to remove extra new lines???
1
2243
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 output i would expect is as follows:
7
4999
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 and, after submitting via POST to a preg_replace filter, get back '_sometext_' (i.e., the same thing with the literal backslashes stripped)
2
7137
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 "stripnig" text that it looks (or "cames to mysql db) like: Lorem ipsum \"
1
1300
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 ¡¦¬£ÓÑÊ 2) letters ()-_=+!@#$%^&*`~',/\|. I have for example such varaible
4
215
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 a form field value to the '(###) ###-####' U.S. phone format. My question, can I combine them into one? $phone=preg_replace('/\D/','',$text); // strip all non-digits $phone=preg_replace('/^/','',$text); // strip leading 0 or 1...
1
1963
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 get +1 than what it should be. e.g. I have in my array A,B,C the count would return 3 which is correct but I have my data like this A,B,C, this adds 1 to the count because there is a comma on the end. I jus need to know how to do a...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.