php - Getting count of spaces before a placeholder with regex selects too many lines

236

I want to create a simple PHP-Template system where a placeholder in a HTML-Page gets replaced with a dynamic generated content. I want to add to each line at the beginning of the dynamically created content as many spaces as there are before my placeholder. The problem is that my regex selects more lines than one (for get the cound of spaces before the placeholder; see graphic at the end).

This is a sample template with the placeholder. I need to cound how many spaces are between the \n and the beginning of my placeholder. In this case, there are no spaces between the beginning of the line an the beginning of my placeholder.

<p>Somethin before</p>

<!--#::CONTENT#-->

<p>Something after>

And this is my regex I use for counting how many spaces are before the placeholder. I made a group that only gives me the spaces without the \n and the placeholder. \s is a escaped space. I dont support TABS.

\n(\s*)<!--#::CONTENT#-->

Regular expression visualization

Debuggex Demo

As you can see in this example, there are three lines selected. Yellow is the part that matches my regex and orange is the group i want to get (only the spaces).

Example of my template

Now my question: Why does this regex select more lines than one? I only allow \s 0 ore more tims between the new line and my placeholder. how can a \n between match the regex? And do I have to change to make it work?

Here is how I use the regex in my php page:

//THIS WILL BE THE DYNAMICALLY CREATED CONTENT OF MY PAGE
$pageContent = getIncludeContents('templates/test.php');

//THIS IS THE LAYOUT THAT IS THE SAME ON ALL PAGES
$layoutContent = getIncludeContents('templates/layout.php');

//Here I try to find how many spaces are before my placeholder
preg_match("/\\n(\\s*)<!--#::CONTENT#-->/", $layoutContent, $matches);

//Check if placeholder was found
if(count($matches) == 0 || count($matches) == 1) {
    die('No MATCHES');
} else if(count($matches) == 2) {
    $indent_space = $matches[1];
} else {
    die('Too Many matches! BUG?');
}

//Now I add to every new line the spaces
$pageContent = str_replace("\n", "\n" . $indent_space, $pageContent);

//And finally I insert the dynamic content
echo str_replace("<!--#::CONTENT#-->", $pageContent, $layoutContent);

If my problem isn't clear enough explaned, please comment my question.

954

Answer

Solution:

It is because\s matches newlines too. To solve this problem use\h instead (for horizontal spaces).

772

Answer

Solution:

The\s shorthand character class actually is equivalent to the character class[ \t\r\n\f] so it will match newlines if it can... If you want to get only one line of spaces, use this:

\n( *)\s*<!--#::CONTENT#-->

regex101 demo

548

Answer

Solution:

and orange is the group i want to get (only the spaces).


\s is the whitespace class, it includes newlines, carriage returns, tabs, spaces etc...

Use your same regex, After you get the whitespaces, you have to remove all but
the actual spaces, then count the length of that string to get your space count.

People are also looking for solutions to the problem: php - What's wrong with this update query?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.