0

I have a open source editor on the cms that I am making that automatically inserts a

<br />

tag at the beginning of the post it submits to the database. This makes validation a pain, since even though there is no real text being submitted, the form still accepts the break tag as input and prevents the "Please enter some text" error from showing.

So I tried to remove the opening break tag by filtering my input like this:

substr($_POST['content'], 6);

This works as long as the user doesn't press the backspace a couple of times which removes the break tag in which case the first 8 characters of the post gets removed even if they are not a break tag.

So how can I remove the first 6 characters of the input ONLY if those first 6 characters are composed of the break tag. Also I don't want to remove all break tags, only the one at the very beginning of the post.

1

6 Answers 6

4

Performant and verbose:

if (substr($_POST['content'], 0, 6) == '<br />') {
    $_POST['content'] = substr($_POST['content'], 6);
}

Not so verbose, but slightly slower:

$_POST['content'] = preg_replace('#^<br />#', '', $_POST['content']);
Sign up to request clarification or add additional context in comments.

5 Comments

Oh, how I miss Python's startswith() method in PHP!
You copied the misplaced closing parenthesis from the question.
@Boldewyn: what keeps you from creating one? I do for my personal projects.
@nikc: I do, too, but still, a default implementation would be a nice extension, particularly with respect to the tons of other functions of less use PHP pumps into the global namespace.
@Boldewyn: I agree completely. str_startswith and str_endswith should exist.
3
$content = $_POST['content'];
if ( substr($content, 0, 6) == '<br />' )
    $content = substr($content, 6);

Comments

3

Creating a workaround for your open source editor is not the solution you should be seeking. Wouldn't it be wiser to just fix the editor so it doesn't insert that <br /> there so you just don't have to worry about it at all?

1 Comment

Let me tell from my experience: It depends. If the editor has this and only this quirk, fixing server-side is a) faster to implement and b) future proof. The problem is, that you have to port fixes to the editor to each new version of the editor you include. In the above case, however, stripping a <br> from the beginning of a string is a reasonable sanitation plus you don't have the fuss when updating the editor software.
2

You can use eg. preg_replace:

$text = preg_replace('#^<br />#', '', $text);

If you don't want to use regexps, you could check with substr before removing:

if (substr($text, 0, 6) == '<br />')
    $text = substr($text, 6);

1 Comment

couldn't you just use preg_match and add an end-of-string anchor after the <br />? Like preg_match('#^<br />$#')
1

Here's what I think to be a better solution of testing whether the user has actually entered something:

if (!strlen(trim(strip_tags($_POST['content'])))) {
    // no content
}
else {
    // some content
}

This basically tests if there is anything other than HTML tags in the input.

1 Comment

Depends on the use case if it is a realistic scenario, but if the content is just an image, this approach will fail.
0

First remove blank spaces at the beginning with ltrim, then test if there is a br. If there is a br remove it and do another ltrim to remove blanks after the br.

$str = ltrim($_POST['content']);
if (substr($str, 0, 6) == '<br />'){
   $str = ltrim(substr($str, 6)); 
}

if ($str){
    //some content
} else {
    //no content
}

In this way you ignore blank spaces and new lines before and after the br.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.