Answer the question
In order to leave comments, you need to log in
Help me write a regular expression to replace the first occurrence of a word in HTML, but strictly outside of anchors and attributes?
Friends, you need to find and replace the regular expression in the text marked up in HTML:
<a>…</a>
(i.e. does not fall into the anchor of any link)alt
the tag type img
)<p>Самые вкусные огурцы росли у меня на даче прошлым летом, это был отличный сезон. Когда я их срывал, то уплетал вот так: <br /> <img src="cucumber-eater.png" alt="Я ем огурец"></p>
<p>Кстати, вы знали, что <a href="http://example.com/super-facts/blue-cucumber" title="По ссылке рассказ про то, как вырастить синий огурец">обычный огурец может быть синим</a>? Я вот — не знал, думал, что они все только зелёные.</p>
<p>Лично мне аппетитным кажется каждый огурец, только что сорванный с грядки. Хотя свежий на вид огурец и зимой можно купить в любом супермаркете, я предпочитаю кушать только то, что выращено своими руками под бдительным контролем.</p>
<a>
). But it would be much more convenient to have a solution in the form of a working regular expression (performance is not critical). I can’t master it, because I’m not familiar with regular expressions. Answer the question
In order to leave comments, you need to log in
/(search)(?!.*(?:)|(".*>))/
This is purely theoretical :)
There is no way to check on the phone at one in the morning)
P.S: search is the search word :)
It is impossible to do this in any regular language. If you are wondering why, read about finite automata (regular expressions are a special case)
Look towards DOM
When a person encounters a problem, they think "I can easily solve this problem with a regular expression!". Since then, he has had two problems...
Posted as requested above.
Let's go to this repository .
We drag three files from there: xmlp.inc , progress.inc and cucumbers.zip .
The example for your question is in cucumbers.zip .
xmlp.inc is a DOM type parser.
progress.inc - just a helper, used by the example to measure and display the time of work.
You need to unzip the zip and copy the remaining two files into the resulting directory.
We actually run the example.php
example.
The main function you need is: replace_text ()
The first two parameters are clear and so - this is the text to search for and what you are looking for.
The fourth parameter $ignore_tags is an array of tag names we are ignoring. In your case, by convention, it's 'A'. 'IMG' in the example can be excluded from this array - I just added )
The third parameter is what to replace the found occurrences with.
But if this third parameter is false (I made the option this way), then the function will return not the changed string, but an array of offsets of the found occurrences.
The function does not stop at the first valid occurrence - it replaces everything it finds and everything that matches the condition.
If you don't want the function to rule HTML/XML jambs to its own understanding, and at the same time want to replace only counted occurrences, then you can get offsets, and then either in a loop replace everything with PHP's substr_replace function(because you have offsets of occurrences, and you also know the length of the searched string), or replace only the first occurrence by the first offset from the returned array.
In the example of the function frt1() , frt2() and frt3() are identical in functionality, frt1() works recursively, in the rest I got rid of recursion. frt3( ) differs from frt2() only in associative stack indexing (not so dazzling and clearer in the eyes). And since all three of these functions do the same thing, the first two can be removed.
It actually uses frt3() for search and replace and frt4() for getting offsets. Cucumbers.txt
file- this is your example, in cucumbers1.txt I stuffed more cucumbers in different places)))
These files are used as input, well, you'll figure it out there, you can see everything in the code.
The results of the work of the instance are also spat into files, you will see them in the same directory after the script has been processed.
There will be questions - ask.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question