Ask Question Forum:
Model Library:2025-02-08:A.I. model is online auto reply
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-19 06:26:28
Point:500 Replies:16 POST_ID:828534USER_ID:11059
Topic:
JavaScript;;PHP Scripting Language
Dear Exprt,
I would like to replace the following string ,
Case-1
url('/images/gradientbottom.jpg') to
url('http://www.example.com/images/gradientbottom.jpg') in preg_replace in php
OR
Case-2
url("/images/gradientbottom.jpg") to
url("http://www.example.com/images/gradientbottom.jpg") in preg_replace in php
OR
case-3
url(images/gradientbottom.jpg) to
url(http://www.example.com/images/gradientbottom.jpg) in preg_replace in php
OR
case-4
url(/images/gradientbottom.jpg) to
url(http://www.example.com/images/gradientbottom.jpg) in preg_replace in php
How to make it? I tried the following regexpression by reg_repace in php for case-1 but it fail it always echo
the same output $buffer as url('/images/gradientbottom.jpg') , any idea ? , it is really hard for regexp
for bracket speical character "(" or "(......0"; Please advise
Duncan
<?php
$url="http://www.example.com";
$buffer=url('/images/gradientbottom.jpg');
$buffer= preg_replace('/(?<=url()[^)]+(?=))/x', 'url()'.$url.'(?=)', $buffer);
echo $buffer; //***********expect result:url('http://www.example.com/images/gradientbottom.jpg')
?>
I would like to replace the following string ,
Case-1
url('/images/gradientbottom.jpg') to
url('http://www.example.com/images/gradientbottom.jpg') in preg_replace in php
OR
Case-2
url("/images/gradientbottom.jpg") to
url("http://www.example.com/images/gradientbottom.jpg") in preg_replace in php
OR
case-3
url(images/gradientbottom.jpg) to
url(http://www.example.com/images/gradientbottom.jpg) in preg_replace in php
OR
case-4
url(/images/gradientbottom.jpg) to
url(http://www.example.com/images/gradientbottom.jpg) in preg_replace in php
How to make it? I tried the following regexpression by reg_repace in php for case-1 but it fail it always echo
the same output $buffer as url('/images/gradientbottom.jpg') , any idea ? , it is really hard for regexp
for bracket speical character "(" or "(......0"; Please advise
Duncan
<?php
$url="http://www.example.com";
$buffer=url('/images/gradientbottom.jpg');
$buffer= preg_replace('/(?<=url()[^)]+(?=))/x', 'url()'.$url.'(?=)', $buffer);
echo $buffer; //***********expect result:url('http://www.example.com/images/gradientbottom.jpg')
?>
Expert: Marco Gasi replied at 2024-11-19 09:49:50
Yes, I really would like you explain that "B" grade: if code works, an "A" has to be given, if it does not you only need to say and to ask for a better solution explaining what is missing in provided solutions.
Expert: Ray Paseur replied at 2024-11-19 09:43:51
What was wrong with the answers? Why did you mark the grade down to a "B" when you got tested and working examples? I do not understand what is wrong. Please explain, thanks.
Author: duncanb7 replied at 2024-11-19 09:36:59
<?php
$url="http://www.example.com";
$buffer="url('/images/gradientbottom.jpg')";
if (strpos($buffer, '"') !== false){
$buffer= preg_replace('/(url(")/x', 'url("' . $url, $buffer);
}elseif (strpos($buffer, "'") !== false){
$buffer= preg_replace('/(url(')/x', 'url('' . $url, $buffer);
}else{
$buffer= preg_replace('/(url()/x', 'url(' . $url, $buffer);
}
echo $buffer;
?>
it works for my case
$url="http://www.example.com";
$buffer="url('/images/gradientbottom.jpg')";
if (strpos($buffer, '"') !== false){
$buffer= preg_replace('/(url(")/x', 'url("' . $url, $buffer);
}elseif (strpos($buffer, "'") !== false){
$buffer= preg_replace('/(url(')/x', 'url('' . $url, $buffer);
}else{
$buffer= preg_replace('/(url()/x', 'url(' . $url, $buffer);
}
echo $buffer;
?>
it works for my case
Author: duncanb7 replied at 2024-11-19 09:36:19
thanks for your reply
Expert: Marco Gasi replied at 2024-11-19 09:29:54
@Duncan, have you tried the code in the comment ID:37164319? It seems you have totally forgot it :-)
Author: duncanb7 replied at 2024-11-19 09:25:58
but it is not useful from <base> if some links are
".../images/a.gif" and "/image/b.gif" existing at the same time in one html page
".../images/a.gif" and "/image/b.gif" existing at the same time in one html page
Expert: Ray Paseur replied at 2024-11-19 08:00:53
See if any of these links give you an idea. You may not need to do an extensive amount of editing!
http://lmgtfy.com/?q=base+href
http://lmgtfy.com/?q=base+href
Author: duncanb7 replied at 2024-11-19 07:41:10
Where x Allows you to use white space in the expression for clarity
What is white space ? why you put x in the pattern ?
Please advise
Duncan
What is white space ? why you put x in the pattern ?
Please advise
Duncan
Author: duncanb7 replied at 2024-11-19 07:37:41
I am using php's curl to get other domain website page and echo result into my iframe. So I need
to replace all partial link in href=, src=, ulr(, etc... to be full domain and path name to complete the
webpage display in my iframe since it is cross-domain issue. Beside using preg_replace(), do you think
we can do that in alterative way. I have some php proxy article in which nothing related to my problem.
Anyway, thanks for your reply and I am trying hard to study preg_replace, the rule of regex.
Duncan
to replace all partial link in href=, src=, ulr(, etc... to be full domain and path name to complete the
webpage display in my iframe since it is cross-domain issue. Beside using preg_replace(), do you think
we can do that in alterative way. I have some php proxy article in which nothing related to my problem.
Anyway, thanks for your reply and I am trying hard to study preg_replace, the rule of regex.
Duncan
Assisted Solution
Expert: Marco Gasi replied at 2024-11-19 07:28:37
250 points GOOD
I tested this:
<?php$url="http://www.example.com";$buffer="url('/images/gradientbottom.jpg')";if (strpos($buffer, '"') !== false){ $buffer= preg_replace('/(url(")/x', 'url("' . $url, $buffer);}elseif (strpos($buffer, "'") !== false){ $buffer= preg_replace('/(url(')/x', 'url('' . $url, $buffer);}else{ $buffer= preg_replace('/(url()/x', 'url(' . $url, $buffer);}echo $buffer;?> 1:2:3:4:5:6:7:8:9:10:11:12:13:
Cheers
Accepted Solution
Expert: Ray Paseur replied at 2024-11-19 07:06:45
250 points GOOD
Given what we have to work with, this shows the general design. Add other <div> tags after line 7 to expand the scope of your tests. If your HTML is valid, you would only ever need to use single quotes inside the :url() part of the tag. The code works correctly with the one line of test data, but for obvious reasons (if it were my task) more tests would be better.
To see the general thought processes that go into something like this, please read the article on test-driven development.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
Best regards, ~Ray
To see the general thought processes that go into something like this, please read the article on test-driven development.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
Best regards, ~Ray
<?php // RAY_temp_duncanb7.phperror_reporting(E_ALL);echo "<pre>";// TEST DATA$strs = <<<ENDSTRS<div style="width:100%;clear:both;margin:0;padding:0;background-color:transparent;background-image:url('/images/gradientbottom.jpg');background-repeat:repeat-x;position:relative;top:65px;">ENDSTRS;// THE FULLY QUALIFIED DOMAIN$url = 'http://example.com';// MAKE A REGEX TO ISOLATE THE URL$rgx= '#' // REGEX DELIMITER. '^' // START OF STRING. '(' // START GROUP. '.*?:url(' // ANYTHING THROUGH "URL(" WITH PAREN ESCAPED. ')' // END GROUP. '"?' // OPTIONAL QUOTE. "'?" // OPTIONAL APOSTROPHE. '(.*?)' // GROUP OF ANYTHING. '"?' // OPTIONAL QUOTE. "'?" // OPTIONAL APOSTROPHE. '().*?)' // GROUP THROUGH END OF STRING WITH LEADING PAREN ESCAPED. '$' // END OF STRING. '#' // REGEX DELIMITER;// TRIM AWAY NOISE AND MAKE AN ARRAY FROM THE LINES$arr = explode(PHP_EOL, trim($strs));// ITERATE OVER THE ARRAY TO EXTRACT THE PARTSforeach ($arr as $str){ preg_match($rgx, trim($str), $mat); // ACTIVATE THIS LINE TO SEE WHAT THE REGEX DID // var_dump($mat); // REASSEMBLE THE MATCHED PARTS WITH THE DOMAIN $new[] = $mat[1] . "'" . $url . $mat[2] . "'" . $mat[3] ;}// SHOW THE WORK PRODUCTecho PHP_EOL . htmlentities($strs);echo PHP_EOL;foreach ($new as $div){ echo PHP_EOL . htmlentities($div);} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:
Expert: Ray Paseur replied at 2024-11-19 06:44:19
@duncanb7: It might help to have a better, more truly representative set of test cases. Please post those in the code snippet, thanks.
Expert: Marco Gasi replied at 2024-11-19 06:40:42
Is it mandatory to use regex? Could you consider to make a simple string concatenation? I mean:
$url="http://www.example.com";
$buffer='/images/gradientbottom.jpg';
$output = $url . $buffer;
echo $output;
$url="http://www.example.com";
$buffer='/images/gradientbottom.jpg';
$output = $url . $buffer;
echo $output;
Author: duncanb7 replied at 2024-11-19 06:34:23
it is css syntax to call image link for background-images
Author: duncanb7 replied at 2024-11-19 06:33:01
it is coming from my HTML code
<div style="width:100%;clear:both;margin:0;padding:0;background-color:transparent;background-image:url('/images/gradientbottom.jpg');background-repeat:repeat-x;position:relative;top:65px;">
<div style="width:100%;clear:both;margin:0;padding:0;background-color:transparent;background-image:url('/images/gradientbottom.jpg');background-repeat:repeat-x;position:relative;top:65px;">
Expert: ansudhindra replied at 2024-11-19 06:31:46
Hello duncabd7, in your code
$buffer=url('/images/gradientbottom.jpg');
url(..) : is it a string or url is your function??
$buffer=url('/images/gradientbottom.jpg');
url(..) : is it a string or url is your function??