Finding and removing line(s)

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
AndrewBacca
Forum Commoner
Posts: 62
Joined: Thu Jan 30, 2003 10:03 am
Location: Isle of Wight, UK

Finding and removing line(s)

Post by AndrewBacca »

Hey, I haven't posted for a while so hello all.

The problem I have is I need to find

Code: Select all

[EDIT?titles=header,Main section,footer,'']
which is at the top of several pages and I need to replace it with nothing but on

Code: Select all

[EDIT?titles=
and

Code: Select all

]
are the same each time the middle will check on each page and I am useless at redex things! so I need some help please :)

Andrew :)

P.S this is what I have got at the moment but it doesn't work

Code: Select all

$this->output =  str_replace('#\[EDIT?title([A-Za-z0-9])\]#', "", $this->output);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_replace() isn't a regex function. :P

This may work.

Code: Select all

$this->output = preg_replace('#^\s*\[edit\?titles=.*\]\s*$#im', '', $this->output);
User avatar
AndrewBacca
Forum Commoner
Posts: 62
Joined: Thu Jan 30, 2003 10:03 am
Location: Isle of Wight, UK

Post by AndrewBacca »

hey, thanks for the fast reply and that proves my point about not knowing what I am doing lol

but im sorry to say that doesnt do a sauage :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It works for me and works as you've specified, it finds and removes sole lines that contain the pattern you've shown.

Code: Select all

[feyd@home]>php -r "$str = '  [edit?titles=asdfasdjfjfjfjfj]  jfjfjf]  ' . PHP_EOL . 'j [edit?titles=asdffff]' . PHP_EOL; echo preg_replace('#^\s*\[edit\?titles=.*\]\s*$#im', '', $str);"

j [edit?titles=asdffff]
User avatar
AndrewBacca
Forum Commoner
Posts: 62
Joined: Thu Jan 30, 2003 10:03 am
Location: Isle of Wight, UK

Post by AndrewBacca »

cheers feyd I put it in the wrong function :)

Thanks :D
User avatar
AndrewBacca
Forum Commoner
Posts: 62
Joined: Thu Jan 30, 2003 10:03 am
Location: Isle of Wight, UK

Post by AndrewBacca »

me again!

say I had

Code: Select all

blah[!EDIT]blah
how can I make it ignore the blah's either side so the [!EDIT] is removed but they stay, because at the moment the [!EDIT] is not getting removed at all when there is items touching it.

Ta
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

change the "m" at the end of the pattern to an "s", add a "?" after .*, remove the "^" and "$" from the beginning and end respectively.
User avatar
AndrewBacca
Forum Commoner
Posts: 62
Joined: Thu Jan 30, 2003 10:03 am
Location: Isle of Wight, UK

Post by AndrewBacca »

thats the tickets thanks alot
Post Reply