0

I want to match "TEST:" in the following example:

$text = "TEST:

This is a test.";

preg_match_all("/^([A-Z]+)\:$/m", $text, $matches);

But $matches is empty.

This however does work:

$text = "TEST:

This is a test.";

preg_match_all("/^([A-Z]+).*\:*$/m", $text, $matches);

Output:

Array
(
    [0] => Array
        (
            [0] => TEST:
            [1] => This is a test.
        )

    [1] => Array
        (
            [0] => TEST
            [1] => T
        )

)

But I only want it to match "TEST:".

What am I doing wrong here? It seems to have a problem with the colon in the pattern, but doesn't work either if I don't escape it.

Thanks for help!

1 Answer 1

2

Actually when you use $ to denote the end of the line, be VERY carefull from where the string is build: On windows, an end-of-line is \r\n while on unix it is \n only The $ end delimiter ONLY recognize \n as an end of line

$text = "TEST:

This is a test.";
$text = str_replace("\r", "", $text);

preg_match_all("/^([A-Z]+)\:$/m", $text, $matches);

will work perfectly

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it seems that was actually the culprit! Works fine now :)

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.