I want to add my own value to all hyperlinks in a page ... like the links are:
< Pre> & lt; A href = "abc.htm? Val = 1" & gt; ABC 1 & lt; / A & gt; & Lt; Br / & gt; & Lt; A href = "abc.htm? Val = 2" & gt; ABC 1 & lt; / A & gt; & Lt; Br / & gt; & Lt; A href = "abc.htm? Val = 3" & gt; ABC 1 & lt; / A & gt; & Lt; Br / & gt; & Lt; A href = "abc.htm? Val = 4" & gt; ABC 1 & lt; / A & gt; & Lt; Br / & gt;
I want to add all hyperlinks to "var =" type = int "
output should be:
& lt ; A Href = "abc.htm? Val = 1 & amp; Type = int "> ABC1
ABC1
ABC1
ABC1
I hope this is done very easily with the preg_replace function
If this is the only case to attach the variable to the href
attribute , Then it will work:
# v-- & amp; instead preg_replace ('/ (& lt; a \\ s + [^ & Gt;] + href = "[^"] *) (") / ',' $ {1} and type = int $ 2 '$ Page_src);
But if you desire code-hardness, then that code changes in href = "index.php"
in the href = "index If you do not have a .php? Type = int "
and href =" index.php and type = int "
, then you will need to do some additional checks:
< Code> function process_link_match ($ matches) {# get rid of full match string; All the parts are already covered in the array_shift ($ matches); If (preg_match ('/ \\? [^ "/', $ Matches [2])) {# link already contains $ _GET parameter $ matches [1]. = '& Amp; type = int'; } And {# matches a new list of criteria [1]. = '? Type = int';} return implode ($ matches);} $ Page_src = preg_replace_callback ('/ (& lt; a \\) S ^ [^ & gt;] + href = ") ([^" * *) ("[^> gt; * & gt;) / ',' process_link_matches', $ Page_src);
Comments
Post a Comment