V
V
vgrayster2010-11-15 09:48:52
PHP
vgrayster, 2010-11-15 09:48:52

Replacing all external links through a redirect

Good afternoon.

There is a task to make a php filter that receives an html page as input and outputs the same page as output, but all external links change to /away.php?url=blabla

The task itself is not difficult, I will quickly do it through preg_replace, but preg_ has a limit of 100K, if the text is greater than this limit, then the functions do not work, you can of course raise the limit. but I want something more flexible.

What options do I see
1) Parse the DOM tree and change it there, but then non-valid documents will not be processed

2) Break the text into pieces less than 100k and change there, the question is how to break the text so that the border does not pass in the middle of the link

3) To analyze for each line, IMHO, is also not the best option.

Any ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wott, 2010-11-15
@Wott

Of course, it’s crooked, but
preg_match or preg_match_all with a flag to save the offset found,
we change through the str_replace
loop to the next slice from the place of the last offset

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question