splitting a string...
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?
m3rajk posted this at 15:17 — 8th October 2003.
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 posted this at 17:14 — 8th October 2003.
He has: 4,048 posts
Joined: Aug 2000
Try wordwrap().
Renegade posted this at 06:42 — 9th October 2003.
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
Thanks Mark
Renegade posted this at 09:12 — 9th October 2003.
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
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 posted this at 16:01 — 9th October 2003.
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 posted this at 09:39 — 10th October 2003.
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. I used the truncate option you provided which worked
Suzanne posted this at 15:37 — 10th October 2003.
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 posted this at 09:32 — 11th October 2003.
He has: 3,022 posts
Joined: Oct 2002
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?
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.