Jump to content

preg_match textarea with tinymce post - array wrong


quasiman

Recommended Posts

Hey guys, I'm trying to allow users to post their Google Plus game links on my site by just copy pasting them from the stream, and I want to validate what they post before storing it locally.  So this is where I'm at so far...ignore the non secure methods at this point, as I'll deal with that when I'm not working locally....

if(isset($_POST['submit'])) {
$message = $_POST['messagepost'];
$regex = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if(preg_match($regex, $message, $url)) { // $url is now the array of urls
  // here you should check $url content and length
  echo $url[0]; // the first one in this case
  echo "<pre>";
  print_r($url);
  echo "</pre>";
  echo "<hr>";
  echo $message;
}
else {
  // no urls found
  echo "no urls found";
}

 

The problem with this script is that it pulls the URL from the first case, which is an image.  So the variable $url[0] ends up looking like:

https://plus.google.com/games/.....base64encoded params...../source/3"><img 

 

So first I need to remove that last bit of text that should not be there: ("><img)

 

There are two URLs in the feed post, one as an image and the other as a basic link....and from the two I have three array items:

 

Array
(
    [0] => https://plus.google.com/games/.....base64encoded params...../source/3"><img
    [1] => https
    [2] => /games/.....base64encoded params...../source/3"><img
)

 

So this is telling me that my regex is wrong, and it's only splitting up the first url (I only posted one image)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.