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')
?>
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
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??