splitting a string...

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

For the log program that I made a while a go, I would like to add an extra function , and that is taking a string(URL) and splitting it up into chunks because, some of the refferers that I'm getting are incredibly long addressess.

e.g.
http://www.somedomain.net/somefolder/someotherfolder/anothersomefolder/a...
to
http://www.somedomain.net/
somefolder/someotherfolder
/anothersomefolder/another
folderhere/index.html

I would like to split them into chunks of 20 or 25.

I searched for a function to count each character then insert a "" but I guess there isn't one (on PHP.net)

Anyone know how to do this?

They have: 461 posts

Joined: Jul 2003

all i can think of right now is doing a preg split so that you get an array. then do what you want with the array.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Try wordwrap().

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

hmmm, wordwrap() lol never actually thought of that.

Proof that solutions to problems are usually the most simplest Sticking out tongue

Thanks Mark Laughing out loud

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

the wordwrap() function works but (always a but) not for a url, when I do this:

<?php
$text
= \"the quick brown fox jumps over the lazy dog\";
$text2 = wordwrap($text, 25, \"<br />\");
?>

It works, but when I do this:
<?php
$text
= $details[3];
//$details[3] may have a value a bit like this:
//http://localhost/public/matrix/s2/index.php?page=contact
$text2 = wordwrap($text, 25, \"<br />\");
?>

It doesn't Sad

Here is the piece of code concerned:

<?php
for($i = 0; $i < count($details); $i++)
  {
   
$text = wordwrap($details[$i], 25 , \"<br />\n\");
    if(
$i == \"3\" || $i == \"4\")
      {
        echo \"<td><a href=\\"
\" . $details[$i] . \"\\" class=\\"link\\">\" . $text . \"</a></td>\";
      }
    else
      {
        echo \"<td>\" .
$details[$i] . \"</td>\";
      }
  }
?>

Any ideas?

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

I would personally add some link text to the database, as urls are rarely helpful... However, what about doing this:

<?php
for($i = 0; $i < count($details); $i++) {
   
$detailsLen = strlen($details[$i]);
    if (
$detailsLen > 25) {
       
$text = wordwrap($details[$i], 25 , \"<br />\n\");
        // or truncate it like this
        //
$text = substr($details[$i], 0, 25) . '...';
        // which puts three dots at the end
        echo '<td><a href=\"' .
$details[$i] . '\" class=\"link\">' . $text . '</a></td>';
    }
    else {
        echo \"<td>
$details[$i]</td>\";
    }
}
?>

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Thanks Suzanne, the wordwrap still doesn't work for the URLs, if anyone knows why (and maybe provide a way around it) please do share. Smiling I used the truncate option you provided which worked Laughing out loud

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Perhaps it doesn't work because it's all one "word" as far as PHP is concerned, so you'd have to set the cut option to on...

The documentation suggests you'd need this:

<?php
for($i = 0; $i < count($details); $i++) {
   
$detailsLen = strlen($details[$i]);
    if (
$detailsLen > 25) {
       
// the fourth part is the cut flag,
        // which defaults to no cutting of long words
        // so you'd have to set it to true, or 1
       
$text = wordwrap($details[$i], 25 , \"<br />\n\", 1);
        echo '<td><a href=\"' .
$details[$i] . '\" class=\"link\">' . $text . '</a></td>';
    }
    else {
        echo \"<td>
$details[$i]</td>\";
    }
}
?>

But I'm imagining this may cause other problems. I was wondering why you wouldn't have linking text for urls, but then I wondered why you'd have just details and no link and I realized I have no idea what you're building.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Quote: Originally posted by Suzanne
But I'm imagining this may cause other problems. I was wondering why you wouldn't have linking text for urls, but then I wondered why you'd have just details and no link and I realized I have no idea what you're building.

I'm building a log file, well actually, I've built it, its just that sometimes, I get really really long URLs in them.

Heres a link to the actual log file ... notice its from TWF? Sticking out tongue

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.