SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    issue with string replacement

    well, suppose my text is like:

    there was a king who was <img src="images/king.jpg"> and he had a kindom <img src="http://www.abc.com/mysite/images/king.jpg">.

    Now i want to replace images/king.jpg with http://www.abc.com/mysite/images/king.jpg

    i tried using str_replace,
    but my problem is that http://www.abc.com/mysite/images/king.jpg also get replace with http://www.abc.com/mysite/http://www...mages/king.jpg

    I tried something like:

    preg_replace('/images/',"$root/workarea/sites/$siteid/images",$sValue);


    preg_replace('^/images/',"$root/workarea/sites/$siteid/images",$sValue);

    Tried studying regular expressions too, but not able to do this!

    But, nothing works....

    Plz suggest something,
    Thanks,
    Kul.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Personally I'd use a DOM approach, but as for RegEx:
    PHP Code:
    preg_replace('~="/?images~'"=\"{$root}/workarea/sites/{$siteid}/images"$sValue); 
    It simply makes sure 'images' or '/images' is the first part of the argument before replacing
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot buddy, your solution worked perfectly, and saved me a lot of time!

    But, i'll like to study regular expressions as much i can, can u plz suggest me some good resources for that?

    Thanks once again!

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Learn as much as you can from regular-expressions.info. They have alot of useful advise and tips - and a full RegEx tutorial.

    That's where I learned from, and browsing the forums helped a lot.

    It's just a case of practice, practice, practice!

    Btw, a dom approach would be:
    PHP Code:
    <?php
    $DOM 
    = new DOMDocument();
    $DOM->loadHTML('
    <html>
        <head></head>
        <body>
            <img src="some/image/image.jpg" />
            <img src="image/image.jpg" />
            <img src="images/image.jpg" />
            <img src="http://images/image.jpg" />
        </body>
    </html>
    '
    );
    $Start 'images';
    foreach(
    $DOM->getElementsByTagName('img') as $Image){
        
    $Location $Image->GetAttribute('src');
        if(
    strpos($Location$Start) === 0){
            
    $Image->SetAttribute('src'"{$root}/workarea/sites/{$siteid}/{$Location}");
        }
    }
    $HTML $DOM->saveHTML();
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Why did you start a new thread after yesterday's? As I said then, a really very good resource is regular-expressions.info.

    Note that the regular expression kindly provided by arkinstall may cause some peripheral problems; especially if you don't understand fully what it is asking for. For example, there are no guarantees that the attribute value you're trying to match is actually associated with an image element. The pattern only asks for the text "images" (with an optional forward slash before it) to be at the start of the attribute (it mightn't even be an attribute!) yet the HTML could contain other instances of that which aren't images or aren't what you want: src="imagesearch for example. Would you want the pattern to be more strict or is what you've got "good enough"?
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Salathe, well pointed out.

    PHP Code:
    preg_replace('~src="/?images/~'"src=\"{$root}/workarea/sites/{$siteid}/images/"$sValue); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •