Good morning, I have a string in which there are breaks represented as<p>----</p>
. The number of dash is often different. Sometimes there are 4, sometimes 5, etc.
Something like this:
Lorem ipsum
<p>------</p>
Lorem Ipsum
<p>
I would like to write a php preg_replace command to make them all uniform and identical.
This is what I developed, but it doesn't work. Where is the error?
{-code-3}
Moreover there is something more precise rather than{-code-4}
to match only spaces and dashes.
EDIT 1: Thanks to the team in the answer section, I correct the function like this:
{-code-5}
This works fine as long as long the text doesn't have a break between the last dash and{-code-6}
. How can I identify the dashed, spaces and also capture the{-code-7}
?
Example:<p>----- {-code-7}{-code-6}
You don't really need the 3 capture groups. You can match zero or more horizontal whitspace chars using{{-code-3}code{-code-3}1}
and replace that with the fixed replacement of{{-code-3}code{-code-3}2}
Example, where as in your code there is always a{-code-3}
at the start and end:
$item = <<<DATA
Lorem ipsum
<p>{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}</p>
Lorem Ipsum
<p>{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}</p>
Lorem Ipsum
<p>{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3}{-code-3} {-code-3}{-code-3}{-code-3}{-code-3}{-code-3}</p>
DATA;
$search = "~<p>{-code-3}{{-code-3}code{-code-3}1}{-code-3}</p>~";
$replace = "{{-code-3}code{-code-3}2}";
$item = preg_replace($search, $replace, $item);
echo $item;
Output
Lorem ipsum
{{-code-3}code{-code-3}2}
Lorem Ipsum
{{-code-3}code{-code-3}2}
Lorem Ipsum
{{-code-3}code{-code-3}2}
You may try finding the pattern and then replace with however many dashes you want, e.g.
{-code-2}
:
$input = "Lorem ipsum \n<p>------</p>\nLorem Ipsum \n<p>
This prints:
Lorem ipsum
<p>-----</p>
Lorem Ipsum
<p>-----</p>
Lorem Ipsum
<p>-----</p>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.