Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-11-18 03:54:46
Point:500 Replies:11 POST_ID:828532USER_ID:11059
Topic:
JavaScript;;PHP Scripting Language
I would like to replace the string href=" by 'hef="http://www.example.com/ by using
preg_replace() in php in following $buffer string but hef="http is ignored to be replaced.
Convert
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="after.html#FIRST-LETTER"></a>';
to final result by preg_replace()
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="http://www.example.com/after.html#FIRST-LETTER"></a>';
How to do that ? Please advise
Duncan
preg_replace() in php in following $buffer string but hef="http is ignored to be replaced.
Convert
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="after.html#FIRST-LETTER"></a>';
to final result by preg_replace()
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="http://www.example.com/after.html#FIRST-LETTER"></a>';
How to do that ? Please advise
Duncan
Author: duncanb7 replied at 2024-11-18 07:27:29
$buffer=preg_replace("/(href=")(?!(http|'+))/",'href="'.$url.'/', $buffer);
Final
Duncan
Final
Duncan
Author: duncanb7 replied at 2024-11-18 07:26:34
$buffer=preg_replace("/(href=")(?!(http|'+))/",'href="'.$url.'/', $temp);
finally it works using above code to ignore href="http in html code and ignore href="'+ which is used inside <script></script>
Duncan
finally it works using above code to ignore href="http in html code and ignore href="'+ which is used inside <script></script>
Duncan
Accepted Solution
Author: duncanb7 replied at 2024-11-18 07:25:54
$buffer=preg_replace("/(href=")(?!(http|'+))/",'href="'.$url.'/', $temp);
finally it works using above code to ignore href="http in html code and ignore href="'+ which is used inside <script></script>
Duncan
finally it works using above code to ignore href="http in html code and ignore href="'+ which is used inside <script></script>
Duncan
Assisted Solution
Expert: Ray Paseur replied at 2024-11-18 05:46:23
125 points EXCELLENT
Right. Now what are the rules and where would we find the test data?
Author: duncanb7 replied at 2024-11-18 04:54:30
Sorry it is href=" not hef=" typing mistake
Assisted Solution
Expert: Ray Paseur replied at 2024-11-18 04:30:29
125 points EXCELLENT
Here is the man page for htmlentities(); it makes the HTML strings print so they can be read on the screen, instead of interpreted into HTML by the browser.
http://us.php.net/manual/en/function.htmlentities.php
href="http://ant4css.blogspot.com/favicon.ico'" contains an extra apostrophe, so it causes a PHP parse error (that's one of the good reasons to read the article I posted above).
Your code sample in the question used two different attribute tags. One of them said "href=" and the other said "hef=" so the target and replacement string were defined with "hef=."
For better or worse, computer programming is a precise science, one that requires a great deal of clarity of thought and precision in written representation. The example in the question does not really give us enough to go on - in the real world, you will probably not encounter the "hef=" attribute, but if you do, it will not matter. Bogus HTML tag attributes will be ignored by the browser. Probably what you want is to replace something around the "href=" attribute. But to know exactly what to replace, we would have to know the rules about what you expect for input and what you want to get for output. And these rules would have to be implemented in an unambiguous way within the larger context of all the input data. So please tell us a little more, show us the test data in situ, and describe the rules you want to apply when replacing some parts of the URL in some of the "href=" attributes of the HTML.
http://us.php.net/manual/en/function.htmlentities.php
href="http://ant4css.blogspot.com/favicon.ico'" contains an extra apostrophe, so it causes a PHP parse error (that's one of the good reasons to read the article I posted above).
Your code sample in the question used two different attribute tags. One of them said "href=" and the other said "hef=" so the target and replacement string were defined with "hef=."
For better or worse, computer programming is a precise science, one that requires a great deal of clarity of thought and precision in written representation. The example in the question does not really give us enough to go on - in the real world, you will probably not encounter the "hef=" attribute, but if you do, it will not matter. Bogus HTML tag attributes will be ignored by the browser. Probably what you want is to replace something around the "href=" attribute. But to know exactly what to replace, we would have to know the rules about what you expect for input and what you want to get for output. And these rules would have to be implemented in an unambiguous way within the larger context of all the input data. So please tell us a little more, show us the test data in situ, and describe the rules you want to apply when replacing some parts of the URL in some of the "href=" attributes of the HTML.
Author: duncanb7 replied at 2024-11-18 04:22:46
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27454454.html
I have other thread for that using str_replace but not working when it is at this
situation.
Convert
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a href="after.html#FIRST-LETTER"></a>';
to final result by preg_replace()
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a href="http://www.example.com/after.html#FIRST-LETTER"></a>';
So I might need to regexpression with preg_replace
I have other thread for that using str_replace but not working when it is at this
situation.
Convert
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a href="after.html#FIRST-LETTER"></a>';
to final result by preg_replace()
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a href="http://www.example.com/after.html#FIRST-LETTER"></a>';
So I might need to regexpression with preg_replace
Author: duncanb7 replied at 2024-11-18 04:19:26
Sorry it is href=" not hef=" typing mistake
Convert
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a href="after.html#FIRST-LETTER"></a>';
to final result by preg_replace()
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a href="http://www.example.com/after.html#FIRST-LETTER"></a>';
Convert
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a href="after.html#FIRST-LETTER"></a>';
to final result by preg_replace()
$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a href="http://www.example.com/after.html#FIRST-LETTER"></a>';
Author: duncanb7 replied at 2024-11-18 04:14:38
Dear ray,
But why href="http://ant4css.blogspot.com/favicon.ico'"
won't be replace d by href="http://www.example.com/http://ant4css.blogspot.com/favicon.ico'"
even it hsas href=" pattern ?
And what is htmlentities($finish) oding for us ?
Please advise
Duncan
But why href="http://ant4css.blogspot.com/favicon.ico'"
won't be replace d by href="http://www.example.com/http://ant4css.blogspot.com/favicon.ico'"
even it hsas href=" pattern ?
And what is htmlentities($finish) oding for us ?
Please advise
Duncan
Assisted Solution
Expert: Ray Paseur replied at 2024-11-18 04:04:15
125 points EXCELLENT
This does what you are asking for, but I think we need to refine the question a little bit.
http://www.laprbass.com/RAY_temp_duncanb7.php
You might want to read this article and then post back with some test data examples. Then we can show you a more appropriate rules-based solution set.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
http://www.laprbass.com/RAY_temp_duncanb7.php
You might want to read this article and then post back with some test data examples. Then we can show you a more appropriate rules-based solution set.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
<?php // RAY_temp_duncanb7.phperror_reporting(E_ALL);/* PROBLEM DEFINITION FROM EE CONTAINS A PARSE ERROR$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="after.html#FIRST-LETTER"></a>';to final result by preg_replace()$buffer='<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="http://www.example.com/after.html#FIRST-LETTER"></a>';*/// STARTING DATA$buffer='<link href="http://ant4css.blogspot.com/favicon.ico" rel="icon" type="image/x-icon"/> <a hef="after.html#FIRST-LETTER"></a>';// TARGET$old = 'hef="';// STRING TO INSERT$new = 'hef="http://www.example.com/';$finish = str_replace($old, $new, $buffer);echo htmlentities($finish); 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:
Assisted Solution
Expert: gplana replied at 2024-11-18 04:03:11
125 points EXCELLENT
Why don't you do a str_replace instead of preg_replace ?
Something like:
str_replace('<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="','<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="http://www.example.com/',$buffer);
Here is the str_replace function specification:
http://php.net/manual/es/function.str-replace.php
Hope it helps.
Something like:
str_replace('<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="','<link href="http://ant4css.blogspot.com/favicon.ico'" rel="icon" type="image/x-icon"/> <a hef="http://www.example.com/',$buffer);
Here is the str_replace function specification:
http://php.net/manual/es/function.str-replace.php
Hope it helps.