-1

I have this message (without the quotes, it's just for being precise):

"hey, here i am<br /><br />
 "

Note the white space after the line break. So here's the thing: I'm trying to remove all the invisible chars and the <br /> of the message, all of them at the end of the message, with a regex to have something like "hey, here I am". But I must do something wrong because I can't make it work. That's what I tried:

$content = preg_replace('{(<br(\s*/)?>|&nbsp;|\r\n|\r|\n| )+$}i', '', $content);

But the message remains the same at the end. Must be something simple I missed. Thank you for your help!

Madnx
  • 1,532
  • 4
  • 16
  • 23

5 Answers5

3

You don't need a regular expression to do that. Use the strip tags function to remove the tags.

$str = 'hey, here i am<br /><br />';
echo strip_tags($str);//yields hey, here i am

Don't try to write your own regular expressions to parse HTML when you have tools that already do it. Sometimes it's necessary depending on case, but in your case I would say it isn't. Just use the built in function.

Zarathuztra
  • 3,215
  • 1
  • 20
  • 34
  • I can't use stri_tags because there is some HTML in the messsage, it is posted by the user with a WYSIWYG editor. EDIT : Oh just remembered I could use strip_tags with only some tags. I'm gonna try that. – Madnx Aug 05 '14 at 01:23
  • That's what "allowed tags" is for. – Zarathuztra Aug 05 '14 at 01:23
  • Well I'm already using HTMLPurifier to indicate which tags should be allowed, will be duplicated code. – Madnx Aug 05 '14 at 01:25
  • @Zarathuztra: You meant probably `$allowable_tags` argument to `strip_tags()` function, as mentioned on [official docs page](http://php.net/manual/en/function.strip-tags.php), right? – Tadeck Aug 05 '14 at 01:25
  • Oh I'm tired (sorry just come from the airport) and forgot to say I want to allow
    , just not at the end of the message.
    – Madnx Aug 05 '14 at 01:26
  • What editor are you using? Some of them allow you to customize exactly how the tags are used. CKEditor being one of them. – Zarathuztra Aug 05 '14 at 01:28
  • I'm using CKEditor, now I wonder if I shouldn't use

    tags instead of
    . Or if there's another solution I'd be interested to hear :).

    – Madnx Aug 05 '14 at 01:29
  • @Zarathuztra Even if I shouldn't use
    , would you know why the line break doesn't disappear at the end? Thank you ;).
    – Madnx Aug 05 '14 at 01:37
  • You'd need to consult the CKEditor documentation. I use it, but am by no means an expert. – Zarathuztra Aug 05 '14 at 01:51
1

You should not use regular expression to do what you wanted. Take a look at this answer: RegEx match open tags except XHTML self-contained tags

Instead use strip_tags().

Community
  • 1
  • 1
Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • I upvoted but I don't understand where it comes from too. The link you gave me was pretty interesting. – Madnx Aug 05 '14 at 02:29
1

You can use the following regex:

([^\s\w",](?:br\W*\s*)+)"$

Working demo

enter image description here

The code is:

$re = "/([^\\s\\w\\",](?:br\\W*\\s*)+)\\"$/"; 
$str = "\"hey, here i am<br /> test<br /><br />\n \""; 
$subst = ''; 

$result = preg_replace($re, $subst, $str);
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • Works but not only with
    :/.
    – Madnx Aug 05 '14 at 02:30
  • @Codel96 can you post some samples of what you need? Check with this regex adding `br`: `([^\s\w",]br.*\s*)` – Federico Piazza Aug 05 '14 at 03:43
  • I'd like to remove all of the
    , spaces and line breaks only at the end of the string. Your code works but it takes everything after the <.
    – Madnx Aug 05 '14 at 06:37
  • @Codel96 can you check this: http://regex101.com/r/vM2rL7/3. By the way, if that doesn't match your need can you update that page and give me the link so I can help you with sample data – Federico Piazza Aug 05 '14 at 15:02
  • @Codel96 ok, check this one: http://regex101.com/r/vM2rL7/5. It takes the br at the ends as you mentioned – Federico Piazza Aug 05 '14 at 16:34
  • First, thank you for the time you're spending on it. Here's what I tested: `$content = preg_replace('#([^\s\w",](?:br\W*\s*)+)"$#', '', $content);` but didn't work. – Madnx Aug 05 '14 at 16:49
  • @Codel96 check the link I gave you and take a look at the section `Code generator`. Then copy/paste the php code. You should use `$re = "/([^\\s\\w\\",](?:br\\W*\\s*)+)\\"$/"; `. Btw, if my support helps you an upvote would be appreciated :) – Federico Piazza Aug 05 '14 at 17:01
  • Oh sorry I scroleld fast and thought I already upvoted. Done. – Madnx Aug 05 '14 at 17:02
  • @Codel96 did it work? Let me know so I can update the answer with solution – Federico Piazza Aug 05 '14 at 17:04
  • I tried `$re = '/([^\\s\\w\\",](?:br\\W*\\s*)+)\\"$/';` but still doesn't work. Maybe comes from the string but I can't see anything wrong with it. – Madnx Aug 05 '14 at 17:06
  • @Codel96 I think that something is wrong in your code. Does this link contain what you want from a regex http://regex101.com/r/vM2rL7/5 ? – Federico Piazza Aug 05 '14 at 17:08
  • Yes, it does. The string is inserted into the database but remains the same if I use a regex or not. – Madnx Aug 05 '14 at 17:31
  • @Codel96 in that case then you should mark this question as solved since my answer solved your question, and open a new one related to your new problem. I've updated the answer with the fix for your question. Btw, when you create the new question send me the link. – Federico Piazza Aug 05 '14 at 17:45
0
$str = 'hey, here i am<br /><br />';
$str=~s{(<br />|\s)*$}{}ig;

use this code this might help you

parthi
  • 156
  • 1
  • 8
0
my $cnt;    
$cnt = "hey, here i am<br /><br />";
    $cnt =~s/(<br \/>)*//isg;
print $cnt;
 output : "hey, here i am"
depsai
  • 405
  • 2
  • 14
  • 1
    Good answers accompany code samples with an explanation for future readers. While the person asking this question may understand your answer, explaining how you arrived at it will help countless others. – Stonz2 Aug 05 '14 at 17:05
  • @Stonz2 Thank you for taking the time to properly review and provide helpful comments. It really does help. –  Aug 05 '14 at 17:28