SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stepping through and replacing in a string.

    I'm trying to take a long string (an HTML file to be exact) and replace a specific tag with a different random string each time it's encountered. I'm assuming the best way to do this is by stepping through the string with a positioning function (strpos?) and calling a new instance of str_replace each time the tag comes up, but I'm pretty unsure on how to go about doing so. Any suggestions? Maybe an easier route? Thanks in advance.

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm..here's an idea in pseduo code:
    Code:
    numofmatches = substr_count(haystack, needle);
    for (i = 0 ; i < numofmatches; i++) {
      randomstring[] = createrandomstring;
    }
    foreach (randomstring as astring) {
      preg_replace(needle, astring, haystack, 1);
    }
    The key is the limit the preg_replace to one match with that fourth parameter...

    That's just off the top of my head. Could be a far more efficient way to do it after a little thinking....
    Matt Wade
    http://codewalkers.com/ - Home of the PHP Coding Contest
    http://phphosts.codewalkers.com/ - Find a PHP host to meet all your needs. Add a host for FREE.

  3. #3
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a somewhat simplified version of the code I wound up using:

    PHP Code:
    $tag_count=substr_count($html$tag);

    for (
    $i=0;$i<$tag_count;$i++) {
        
    $pos strpos($html$tag);
        
    $html substr_replace($html"<a href=\"".grab_url()."\">"$posstrlen($tag));

    As you can see, each time the special tag is found within the HTML, it's replaced by a link to a different URL randomly pulled from the database. The trick was indeed limiting the code to only replace the first instance of the tag before looping back to grab a new URL.

    Speaking of efficiency, is it a better idea to load the URLs into an array before executing the loop?

  4. #4
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by thekid
    Here's a somewhat simplified version of the code I wound up using:

    PHP Code:
    $tag_count=substr_count($html$tag);

    for (
    $i=0;$i<$tag_count;$i++) {
        
    $pos strpos($html$tag);
        
    $html substr_replace($html"<a href=\"".grab_url()."\">"$posstrlen($tag));

    Speaking of efficiency, is it a better idea to load the URLs into an array before executing the loop?
    I'm still a new geek, but I'm going to say yes, it's better to load all of the URLs into an array and make the grab_url() function just return one URL from the array. That way, you're only making one SQL call instead of one for each instance of what you're searching for.


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
  •