Jump to content

[SOLVED] preg_replace(); help. Possibly regex.


Henaro

Recommended Posts

Hey everyone.  I'm having a bit of trouble with one of my scripts. 

 

The error I'm getting is this:

 

Warning: preg_replace(): Unknown modifier 'w' in html/board/boards/post_topic.php on line 66

 

Here's line 66:

$str = preg_replace ($simple_search, $simple_replace, $str);

 

And here's the code I just added that I am suspicious about:

'/\[yt\]http:\/\/youtube.com/watch?v=(.*?)\[\/yt\]/', 

 

Which goes to:

 

'<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/$1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>',

 

The whole idea is to let users embed youtube videos.  But it seems like it's not working correctly.  :(

 

Here's the whole function:

 

function BBCode ($str) {
        $simple_search = array(
                                '/\[b\](.*?)\[\/b\]/is',                               
                                '/\[i\](.*?)\[\/i\]/is',                               
                                '/\[u\](.*?)\[\/u\]/is',
			'/\[url\=(.*?)\](.*?)\[\/url\]/is',
			'/\[url\](.*?)\[\/url\]/is',
			'/(>>)([0-9]+)/',
			'/\[img\](.*?)\[\/img\]/is', 
			'/\[yt\]http:\/\/youtube.com/watch?v=(.*?)\[\/yt\]/', 
			'/\[em\](.*?)\[\/em\]/is',                               
                                '/\[txt\](.*?)\[\/txt\]/is' 
                                );
        $simple_replace = array(
                                '<strong>$1</strong>',
                                '<em>$1</em>',
                                '<u>$1</u>',
			'<a href="$1">$2</a>',
			'<a href="$1">[link]</a>',
			'<a href=\'index.php#$2\'>$1$2</a>',
			'<a href="$1"><img src="$1" boarder=0 /></a>',
			'<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/$1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>',
			'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="550" HEIGHT="400" id="Embeded File"><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#FFFFFF><EMBED src="$1" quality=high bgcolor=#FFFFFF WIDTH="550" HEIGHT="400" NAME="Embeded File" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>',
                                '<div class="code">$1</div>'
                                );
        // Do simple BBCode's
        $str = preg_replace ($simple_search, $simple_replace, $str);

        return $str;
}

 

 

I can't really think of a solution for this.  It's 1:30AM right now though, so it could be that I'm just tired and not seeing straight.  But I hope someone can help me out. 

 

Thanks,

Henaro

Link to comment
Share on other sites

Hello again~

 

Sorry for resurrecting  this posts, but I thought it would be better than creating a new one. 

 

I seem to have broken it.  I moved the BBCode function onto a file called fun.php.  Which is included correctly on the post_reply file.  But now it seems like it broke the youtube tag (which was working fine with your help).  All of the other BBcode works except for this.  ???

 

Anyone know what might be wrong? '_'

 

Thanks,

Henaro

 

EDIT:

And here's the full code:

<?php
function BBCode ($str) {
        $simple_search = array(
                                '/\[b\](.*?)\[\/b\]/is',                               
                                '/\[i\](.*?)\[\/i\]/is',                               
                                '/\[u\](.*?)\[\/u\]/is',
			'/\[url\=(.*?)\](.*?)\[\/url\]/is',
			'/\[url\](.*?)\[\/url\]/is',
			'/(>>)([0-9]+)/',
			'/\[img\](.*?)\[\/img\]/is', 
			'/\[yt\]http:\/\/youtube.com\/watch\?v=(.*?)\[\/yt\]/', 
			'/\[em\](.*?)\[\/em\]/is',                               
                                '/\[txt\](.*?)\[\/txt\]/is' 
                                );
        $simple_replace = array(
                                '<strong>$1</strong>',
                                '<em>$1</em>',
                                '<u>$1</u>',
			'<a href="$1">$2</a>',
			'<a href="$1">[link]</a>',
			'<a href=\'index.php#$2\'>$1$2</a>',
			'<a href="$1"><img src="$1" boarder=0 /></a>',
			'<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/$1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>',
			'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="550" HEIGHT="400" id="Embeded File"><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#FFFFFF><EMBED src="$1" quality=high bgcolor=#FFFFFF WIDTH="550" HEIGHT="400" NAME="Embeded File" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>',
                                '<div class="code">$1</div>'
                                );
        // Do simple BBCode's
        $str = preg_replace ($simple_search, $simple_replace, $str);

        return $str;
}

?>

Link to comment
Share on other sites

  • 4 years later...

This is pretty old lol but... youtube embed is now running off a iframe.. say someone goes to a youtube video and there is more text after the "video id" like &feature= blah blah blah.. well they copy that and insert it into the forum post using a youtube bbc tag. the video won't work because it didn't remove the text after the video id. now I know I can use str replace and what not to remove specific text, but how would I remove anything after the youtube video id?

Link to comment
Share on other sites

Well say someone inputs this:

 

I got it to remove the beginning (http://www.youtube.com/watch?v=) but what about the ending? (&feature=related)

That needs to be removed, youtube's embedded code doesn't work with &feature &NR etc after its video id anymore.

is there a way to remove the ending of the url so only the video id is left? I think every video id is 11 characters if that helps any.

Link to comment
Share on other sites

Of course.

 

<?php 

$str = 'heres a query in the right order
[youtube]http://www.youtube.com/watch?v=DUyn5qyf_6c&feature=related[/youtube]
and heres one in the wrong order
[youtube]http://www.youtube.com/watch?feature=related&v=DUyn5qyf_6c[/youtube]
both should work fine.';

$expr = '%\[youtube\](?:http://){0,1}(?:www.){0,1}youtube.com/watch\?.*?v=([^&[]++)[^[]*+\[/youtube\]%i';

preg_match_all( $expr, $str, $matches, PREG_SET_ORDER );

print_r( $matches );

?> 

 

HTH.

Link to comment
Share on other sites

So I removed the because I gotta have those removed for the other functions to work but.. its not working since I removed those.

 

<?php

$str = 'heres a query in the right order
[youtube]http://www.youtube.com/watch?v=DUyn5qyf_6c&feature=related[/youtube]
and heres one in the wrong order
[youtube]http://www.youtube.com/watch?feature=related&v=DUyn5qyf_6c[/youtube]
both should work fine.';

$expr = '%\(?:http://){0,1}(?:www.){0,1}youtube.com/watch\?.*?v=([^&[]++)[^[]*+\%i';
$replacement = '${1}1,$3';
$matches = preg_replace($expr, $replacement, $str);

echo $matches;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.