O
O
Optimus2015-05-22 00:06:17
PHP
Optimus, 2015-05-22 00:06:17

How to read a string in php?

I don’t know how to formulate the question exactly, but it turns out like this: is it possible to achieve in php reading the text in a variable in a row and as it reads, if the condition matches, immediately perform some actions.
Those. how a person, when reading and as he reads, immediately highlights something with a marker or immediately crosses out the unnecessary.
Example 1: if the text contains the word php and after it the word java is found somewhere in the text then... delete the previous word php.

<p>Ох уж этот php хотя java адский ад лучше уж пусть php</p>

Example 2: if the text has a heading (construction h1... /h1) and after it somewhere there is a br tag and after it a subheading (h2... /h2) then... remove the br tag between them.
<h1>Заголовок</h1> текст <br> текст <h2>Подзаголовок</h2>

In general, it is clear that you need to grab the required ranges with a regular expression, add them to an array, then look at the elements of the array, replace them, then insert the changed ones back into the text. But firstly, there are quite a lot of unnecessary actions, and secondly, if there are identical elements in the array, then then you will ask where exactly these elements are in the text and what exactly they need to be changed. Maybe I need another programming language? If so, which one?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Slashchinin, 2015-05-22
Pyan @marrk2

PHP is a great language in which you can implement almost any algorithm.
In the first case, you can use the explode function with a space delimiter, get the output array exactly in the order in which the words appear. And then process the array as you like.
In the second case, you can also use explode, only when iterating through the array, take into account the beginning and end of the header, set the start flag and the end flag of the tag.
All in your hands.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question