SRL Archive - Web Development

php regex help!


grats

2013-10-08 06:20:05

[PHP]function BBCode ($string) {

$search = array(

'@\[(?i)b\](.*?)\[/(?i)b\]@si',

'@\[(?i)i\](.*?)\[/(?i)i\]@si',

'@\[(?i)u\](.*?)\[/(?i)u\]@si',

'%(@post#)(\d+)%',

'@\[(?i)img\](.*?)\[/(?i)img\]@si',

'@(?:https://)?(?:http://)?(?:www\.)?(?:youtube\.com/(?:v/|watch\?v=)|youtu\.be/)([\w-]+)(?:\S+)?@si',

'@\[(?i)url\](?:https?:\/\/)?(.*?)\[/(?i)url\]@si'

);

$replace = array(

'<b>\\1</b>',

'<i>\\1</i>',

'<u>\\1</u>',

'<a href="#$2" onclick="qp($2)">$1$2</a>',

'<img src="\\1" onclick="imagesize(this)" alt="~img broke/loading~" style="height:200px" />',

'<br><a href="https://youtu.be/$1" target="_blank">https://youtu.be/$1</a> <span onClick="ytvid(&quot;a$1&quot;)"><img id="a$1img" src="http://img.youtube.com/vi/$1/0.jpg" height="150px" style="display:block;"></span><embed id="a$1vid" width="600" height="425" src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="direct" allowfullscreen="true" style="display:none">',

'<a href="http://\\1" target="_blank">\\1</a>'

);

return preg_replace($search, $replace, $string);

}
function str_insert($str, $search, $insert) {

$index = strpos($str, $search);

if($index === false) {

return $str;

}

return substr_replace($str, $search.$insert, $index, strlen($search));

}

function generateRandomString($length = 5) {

$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

$randomString = '';

for ($i = 0; $i < $length; $i++) {

$randomString .= $characters[rand(0, strlen($characters) - 1)];

}

return $randomString;

}

$post1 = bbcode($post);

$randcode = generateRandomString();

$post1 = str_insert($post1, 'onClick="ytvid(&quot;a', $randcode);

$post1 = str_insert($post1, '<embed id="a', $randcode);

$post1 = str_insert($post1, '"><img id="a', $randcode);

$post1 = preg_replace('/(\r\n|\n|\r)/','<br>',$post1);

[/PHP]


Hello, this is a chunk of code I have used on my custom forums for many months

It works extremely well, please ignore my youtube code part, as it's very good at what it does for my client side javascript for youtube videos & smartphones etc


I need help with getting URL's to auto embed, and youtube videos.. currently if a youtube video is posted, from youtu.be or youtube.com it'll auto embed it with my special stuff, it works amazingly.. however URL's have to be wrapped in the I have thought of ghetto ways of fixing this in the past, however I want to be able to just post any youtube url & it slaps that into the code I have, or if you linked any link to a website (other than youtube) it'll just do a normal
anyone able to help? skype or other places

Brandon

2013-10-08 18:55:55

From: grats
…anyone able to help? skype or other places…




I not a pro at php or anything and my standards might be old (it's been a while) or bad but I'm not sure if your regex for the youtube tag is correct? Well it might be but I couldn't get it to work so that's why I say that.

Here is how I did it (Test.php)..


<?php
$StrToSearch = "http://www.youtube.com/watch?v=2vAWjBmGZAk";

//[NoParse] //Makes villavu's forum not parse my regex as an embedded url :l

$Patterns = array("@(https://)?(http://)?(?:www\.)?(?:(youtube\.com)|(youtu\.be))/(?:v/|watch\?v=)([A-Za-z0-9]+)@si");

//[/NoParse]


//NOTE: You can replace $2 in the below code with "http://" or "https://" in case the user doesn't have that part in the url.

$ObjReplacement = array(
"<object data='$2$3/v/$5' type='application/x-shockwave-flash' wmode='direct' width='425' height='350'>
<param name='src' value='$2$3/v/$5' />
<param name='allowFullScreen' value='true' />
</object>");

$EmbedReplacement = array("<embed src='$2$3/v/$5' type='application/x-shockwave-flash' allowfullscreen='true' wmode='direct' width='425' height='350'></embed>");


print "<h2>Using Object tags:</h2><br />";
print preg_replace($Patterns, $ObjReplacement, $StrToSearch);

print "<br /><br /><h2>Using Embed tags:<h2><br />";
print preg_replace($Patterns, $EmbedReplacement, $StrToSearch);
?>


And it ends up looking like:


grats

2013-10-08 20:25:55

From: Brandon
I not a pro at php or anything and my standards might be old (it's been a while) or bad but I'm not sure if your regex for the youtube tag is correct? Well it might be but I couldn't get it to work so that's why I say that.

Here is how I did it (Test.php)..


<?php
$StrToSearch = "http://www.youtube.com/watch?v=2vAWjBmGZAk";

//[NoParse] //Makes villavu's forum not parse my regex as an embedded url :l

$Patterns = array("@(https://)?(http://)?(?:www\.)?(?:(youtube\.com)|(youtu\.be))/(?:v/|watch\?v=)([A-Za-z0-9]+)@si");

//[/NoParse]


//NOTE: You can replace $2 in the below code with "http://" or "https://" in case the user doesn't have that part in the url.

$ObjReplacement = array(
"<object data='$2$3/v/$5' type='application/x-shockwave-flash' wmode='direct' width='425' height='350'>
<param name='src' value='$2$3/v/$5' />
<param name='allowFullScreen' value='true' />
</object>");

$EmbedReplacement = array("<embed src='$2$3/v/$5' type='application/x-shockwave-flash' allowfullscreen='true' wmode='direct' width='425' height='350'></embed>");


print "<h2>Using Object tags:</h2><br />";
print preg_replace($Patterns, $ObjReplacement, $StrToSearch);

print "<br /><br /><h2>Using Embed tags:<h2><br />";
print preg_replace($Patterns, $EmbedReplacement, $StrToSearch);
?>


And it ends up looking like:



uhh, thanks.. but my youtube has worked fine for the year my forums have used it, I need url & youtube to auto embed, instead of having to put brackets around urls

as I said, the youtube requires the client side javascript, which isn't on this thread

also the method this forum uses, and all other forum softwares for that matter.. to embed youtube videos, is horrid.. I use a more of youtube method, which doesn't load flash for every video on a page & crash them. It loads the picture and when you click it, the video then plays... this is the main goal of my youtube, which, needs no fixing

Brandon

2013-10-08 21:56:41

From: grats
uhh, thanks.. but my youtube has worked fine for the year my forums have used it, I need url & youtube to auto embed, instead of having to put brackets around urls

as I said, the youtube requires the client side javascript, which isn't on this thread

also the method this forum uses, and all other forum softwares for that matter.. to embed youtube videos, is horrid.. I use a more of youtube method, which doesn't load flash for every video on a page & crash them. It loads the picture and when you click it, the video then plays... this is the main goal of my youtube, which, needs no fixing



I'm confused by youtube and url's auto embedding. Doesn't your youtube auto embed already? Then the only problem is URLs? Or is the url regex matching with the youtube one as well?

Not sure what "exactly" it is you need but does the below help at all?


$SomeUserPost = "testing url stuff http://villavu.com/forum/member.php?u=8135 other stuff here..";

$Patterns = array("@(^|\s+)((http://)|(https://)|(mailto:)|(ftp://)|(www\.))([A-Za-z0-9!\*'\(\);:\@\&=\+\$\,/\?\#\[\]-_\.~]+)(\s|$)@si");

$Replacements = array(" <a href='$0' target='_blank'>Link Here</a> ");

print "<br /><br /><br /><br />";
print preg_replace($Patterns, $Replacements, $SomeUserPost);


If I have to guess again, I think what you really want is to have a tag that contains a hidden video and when a link is clicked, show that video on the page?


In that case, this should work:


<?php
print "<html>\n";
print "<head>\n";
print " <title>Test Page</title>\n";
print " <style>\n";
print " .vis {display:block;}\n .invis {display:none;}\n";
print " </style>\n";
print "<script>";

print "
function show() {
document.getElementById('video').className='vis';
return true;
}

function hide() {
document.getElementById('video').className='invis';
return true;
}

function toggle() {
if (document.getElementById('video').className == 'invis') {
show();
document.getElementById('showhide').innerHTML = \"<a id='showhide' href='#' onclick='return show();'>Click Here To Hide Video</a>\"
}
else {
hide();
document.getElementById('showhide').innerHTML = \"<a id='showhide' href='#' onclick='return hide();'>Click Here To Show Video</a>\"
}
return true;
}

\n";

print"</script>\n";
print "</head>\n";

print "<body>\n\n";


$StrToSearch = "http://www.youtube.com/watch?v=2vAWjBmGZAk";
$Patterns = array("@(https://)?(http://)?(?:www\.)?(?:(youtube\.com)|(youtu\.be))/(?:v/|watch\?v=)([A-Za-z0-9]+)@si");
$EmbedReplacement = array("<embed src='$2$3/v/$5' type='application/x-shockwave-flash' allowfullscreen='true' wmode='direct' width='425' height='350'></embed>");

print "<a id='showhide' href='#' onclick='return toggle();'>Click Here To Show Video</a>";
print "<div id='video' class='invis'>";
print preg_replace($Patterns, $EmbedReplacement, $StrToSearch);
print "</div>";

print "\n</body>\n";
print "</html>";
?>

The above create a toggle link with a video embedded in it starting invisible. On click, the video shows. On click again, it hides.
Only thing that would need to be done is to assign each div a unique id and pass that to the toggle/show/hide dynamically.

If I'm still misunderstanding then I'm not sure what is required :l

grats

2013-10-09 09:29:41

From: Brandon
I'm confused by youtube and url's auto embedding. Doesn't your youtube auto embed already? Then the only problem is URLs? Or is the url regex matching with the youtube one as well?

Not sure what "exactly" it is you need but does the below help at all?


$SomeUserPost = "testing url stuff [url]http://villavu.com/forum/member.php?u=8135[/url] other stuff here..";

$Patterns = array("@(^|\s+)(([url]http://)|(https://)|(mailto:)|(ftp://)|(www\.))([/url][A-Za-z0-9!\*'\(\);:\@\&=\+\$\,/\?\#\[\]-_\.~]+)(\s|$)@si");

$Replacements = array(" <a href='$0' target='_blank'>Link Here</a> ");

print "<br /><br /><br /><br />";
print preg_replace($Patterns, $Replacements, $SomeUserPost);


If I have to guess again, I think what you really want is to have a tag that contains a hidden video and when a link is clicked, show that video on the page?


In that case, this should work:


<?php
print "<html>\n";
print "<head>\n";
print " <title>Test Page</title>\n";
print " <style>\n";
print " .vis {display:block;}\n .invis {display:none;}\n";
print " </style>\n";
print "<script>";

print "
function show() {
document.getElementById('video').className='vis';
return true;
}

function hide() {
document.getElementById('video').className='invis' ;
return true;
}

function toggle() {
if (document.getElementById('video').className == 'invis') {
show();
document.getElementById('showhide').innerHTML = \"<a id='showhide' href='#' onclick='return show();'>Click Here To Hide Video</a>\"
}
else {
hide();
document.getElementById('showhide').innerHTML = \"<a id='showhide' href='#' onclick='return hide();'>Click Here To Show Video</a>\"
}
return true;
}

\n";

print"</script>\n";
print "</head>\n";

print "<body>\n\n";


$StrToSearch = "http://www.youtube.com/watch?v=2vAWjBmGZAk";
$Patterns = array("@([url]https://)?(http://)?(?:www\.)?(?:(youtube\.com)|(youtu\.be))/(?:v/|watch\?v=)([/url][A-Za-z0-9]+)@si");
$EmbedReplacement = array("<embed src='$2$3/v/$5' type='application/x-shockwave-flash' allowfullscreen='true' wmode='direct' width='425' height='350'></embed>");

print "<a id='showhide' href='#' onclick='return toggle();'>Click Here To Show Video</a>";
print "<div id='video' class='invis'>";
print preg_replace($Patterns, $EmbedReplacement, $StrToSearch);
print "</div>";

print "\n</body>\n";
print "</html>";
?>

The above create a toggle link with a video embedded in it starting invisible. On click, the video shows. On click again, it hides.
Only thing that would need to be done is to assign each div a unique id and pass that to the toggle/show/hide dynamically.

If I'm still misunderstanding then I'm not sure what is required :l


what? Are you messing with me or something? I am so confused

yes, the youtube works, as I've said like 10 times.
I want both the youtube and the url to auto embed (the youtube) and auto link (the url) when someone posts them, instead of having to put
on my forum there are no tags for youtube, there are tags for url, I want to have no tags for both."> brackets around them...? I have absolutely no idea how to explain it better


this is a URL
http://youtu.be/pwud6Qh4e_c
it is going to turn into a link when I post, without having to put on brackets

this is a youtube vid, it REQUIRES me to wrap the tags around it
[video=youtube_share;pwud6Qh4e_c]http://youtu.be/pwud6Qh4e_c[/video]


on my forum there are no tags for youtube, there are tags for url, I want to have no tags for both.

Dgby714

2013-10-16 02:25:50

Did you every get this how you wanted?

How I take it you want a regular expression to get URLs out of a post.

Then automaticly insert the [noparse]URL[/noparse] tags if it's not a youtube video link.

If it is a youtube video link add [noparse][youtube]URL[/youtube][/noparse] tags.


I've got to say how you're handling tags is… the harder way?

Also does it actually work in nested tags with incorrect endings?