What are the commas doing to me?!
Posted: Wed Dec 01, 2004 10:01 am
I am trying to allow people to use a code to enter URLs in a text area so that when its regurgitated the URL will be working correctly - similar to the way these forums work in effect. I have got this far:
This seems to work OK unless there is a trailing , or . after the closing [/URL] in which case they , or . is included as part of the URL. Any suggestions as to why?
Code: Select all
<?php
if ((strpos ($post, "[url]") !==FALSE AND strpos ($post, "[/url]") !==FALSE)) { //convert URLs into working hyperlinks
$data = strtok ($post, "]");
while ($data) {
if (strpos ($data, "http://") !==FALSE) {
$data = substr ($data, 4);
$unformatted_URLs = array ($data); //create list of unformatted URLs in post
$formatted_URLs = array ('<a href="' . $data . '">' . $data . '</a>'); //convert each to a working URL
}
$data = strtok (" ");
}
$n = count ($unformatted_URLs);
for ($x = 0; $x<$n; $x++) { //now replace unformatted URL's with working URL's
$post = str_replace ($unformatted_URLs[$x], $formatted_URLs[$x], $post);
}
$post = str_replace ("[URL]", "", $post);
$post = str_replace ("[/URL]", "", $post);
}
?>