SitePoint Sponsor

User Tag List

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

    using preg_replace to replace a string

    Hi,

    I've never used preg_repllace, so, m getting a bit confused to use them, i need to convert anyting like images/kid.jpg in my content to something like:

    http://www.abc.com/mysite/sites/images/kid.jpg

    Here's how i get my content:
    PHP Code:
                  <?php
    $root
    ='http://'.$_SERVER['HTTP_HOST'].'/mysite/'
    $filename "template.php?id=$siteid&currentpage=$pagename";
    $fp=fopen("$root/workarea/sites/$siteid/$currenteditablefile","r");
    while(!
    feof($fp)) 
    $sValue.=fgetc($fp);

    ?>
    Plz suggest me a way to do this,
    Thanks,
    Kul.

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    The code that you posted has nothing to do with your request. Have you made any attempt to do the replacement on your own?
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya, i tried using preg_replace,but couldn't do it, so, didn't paste that code here, just pasted my original code

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Show us what you tried, whether it worked or not, and we can advise from there. Or do you just want a solution handed to you on a plate?
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, since i have no knowledge about preg_replace, i just searched over google, and tried the code by copy/paste, but, couldn't get it, so, i decided to use str_replace, and it worked fine for me, only issue that i am facing is that i am not able to replace two search strings, i.e if the content contains either this or that or both, then replace both.

    here's my code:

    $cmparray=array("images/","./images/");

    $Value = str_replace("$cmparray", "$root/workarea/sites/$siteid/images/", $sValue);

    $sValue comes after reading from external file.

    Hope its fine now

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Try again without the double quotes around $cmparray.
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yea, my mistake...
    but now i am facing the problem that i was actually afraid of, now if the content already has path like:

    http://www.abc.com/mysite/images

    it gets replaced with:

    http://www.abc.com/mysite/http://www.abc.com/mysite/

    Now i fear i have to use preg_replace for exact match.

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Are you familiar with regular expressions at all? I fear just handing over a regular expression for you to use would wind the battle but not the war; I'd rather help teach you something than give copy/paste solutions.

    If you have a spare few minutes, take a look at http://www.regular-expressions.info/ and particularly useful in your case will be anchors.
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


    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);

    But, nothing works....


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
  •