Jump to content
xisto Community
Sign in to follow this  
galexcd

Preg_replace Problems

Recommended Posts

Sigh... preg_replace is getting very annoying for me. Could sombody tell me what I'm doing wrong?

 

Im making a bbcode replace for the forums I made, and preg_replace is being a b****...

 

Right now Im working on the "http://forums.xisto.com/ tag, and I want to check for valid URL's, because idiots in the forum are adding js and stuff in the image tags and messing it all up. So i put a function in the array that holds all of the replacements:

 

$main_search=array('/\[b\](.*?)\[\/b\]/is','/\[i\](.*?)\[\/i\]/is','/\[u\](.*?)\[\/u\]/is','/\[img\](.*?)\[\/img\]/is','/\[url\=(.*?)\](.*?)\[\/url\]/is','/\[url\](.*?)\[\/url\]/is');$main_replace=array('<strong>$1</strong>','<em>$1</em>','<u>$1</u>',img(),urln(),url());$str=preg_replace ($main_search, $main_replace, $str);

My first try was to pass the variable $1 through to the function as an argument, but it gave me all sorts of errors, which i figured out because variables cant start with a number...

 

Now here comes the annoying part:

function img(){$url='$1';$array=explode(".",$url);$ext=strtolower($array[(count($array)-1)]);if($ext=="jpg"||$ext=="gif"||$ext=="png"||$ext=="jpeg"||$ext=="tiff"||$ext=="bmp")return '<img src="$1" style="max-width: 100%">';elsereturn $url;}

Thats what I'm stuck at right now, I tried making the variable url only to see if that might fix it at all, but it dosnt. Bascally the if statement always returns false, when looking at it, I found the problem. explode isn't working. It is setting the variable array to the entire URL not spliting it. So of course the If statement returns false unless the URL is only the word "jpg" or "gif". AHHHHH this is driving me crazy

 

Why is it doing this?

help!

Share this post


Link to post
Share on other sites

Thanks so much!Yeah, I don't know too many of the standard php functions, so I usualy end up having to make my own, usually using str_replace, explode, and a whole bunch of arrays and other variables...

Share this post


Link to post
Share on other sites

On that page I referenced up above, there was a note several scrolls down with the following bit of information on how to capture the Extension from a filename. Here is a snippet to try:

$extension = substr(strrchr($filename, "."), 1);

Share this post


Link to post
Share on other sites

Nope... still no good.Is preg_replace what i really should be using to do bbcode? and if so, how do other forums check for illegal images? How does Xisto's bbcode work?

Share this post


Link to post
Share on other sites

when I first saw this post, I was wondering why there were so many \ and / in

$main_search=array('/\[b\](.*?)\[\/b\]/is','/\[i\](.*?)\[\/i\]/is','/\[u\](.*?)\[\/u\]/is','/\[img\](.*?)\[\/img\]/is','/\[url\=(.*?)\](.*?)\[\/url\]/is','/\[url\](.*?)\[\/url\]/is');

But I think I know why now, but I'm not sure... Could somebody help me out with this please?
Is it so that you must start and end all pieces you want to replace with preg_replace() with a non-alphanumeric character (and that they must be the same)? And that [ ] and / mean something in the statement? So that you must escape them with \ ?
But, if so, what do they mean?
(I'm already sure that .*? is some kind of wildcard. :lol: )

Thanks in advance,

Peroim

Share this post


Link to post
Share on other sites

It is true that variables cannot start with a number, but $1 is not actually a variable; it is simply a backreference for the PCRE functions. The reason, I would imagine, that it wasn't working when you tried to pass it directly to the img() function is because you weren't enclosing it in quotes - eg. img($1) should not work, img("$1") should. Because $1 is not a variable, and because even if it were it is not being declared on a global scope, the img() function has absolutely no access to it at all - so thus by telling the function to deal with '$1', it is dealing with the literal string '$1'.

 

Now, on to what's wrong with your regular expression.

 

Firstly, the 'e' pattern modifier needs to be included when you want any evaluation of code to take place - so for instance '/\[b\](.*?)\[\/b\]/is' should be '/\[b\](.*?)\[\/b\]/ise'.

 

Secondly, the replacements need to be strings - what you have right now are direct references to the functions themselves, meaning that as soon as you create that array the functions are being called, not when preg_replace() is used. So just say the img() function returned 'xyz', then that item in the $main_replace array would have the value of 'xyz'. So what the replacement array should be is: (note that I have seperated the lines just for clarity - you don't have to do this)

 

$main_replace=array('<strong>$1</strong>','<em>$1</em>','<u>$1</u>','img("$1")','urln()','url()');

I don't know what, if any, variables are supposed to be passed to the url() and urln() functions, so they're empty. The reason it needs to be a string is because it is code that is supposed to be evaluated when, and ONLY when, that particular item is being replaced in the subject - so by calling the functions during the creation of the array, you are effectively defeating the purpose of the script.

 

Hope that helps. Feel free to ask any questions or queries I didn't clear up.

 

Oh, and peroim, the reason there are so many slashes (/) in the pattern is because it is the delimiting character for the patterns, as well as used in the closing tags for bbCode. The reason for so many backslashes (\) is because this is how you 'escape' a character - ie. if a character performs a certain function but you just want that actual character to be displayed or used rather than the special function it may otherwise reference, you escape it. For instance, in the PCRE functions, an opening square bracket specifies the start of a character class, and a closing square bracket the end - so if you want to process an actual square bracket, you need to escape it.

Edited by Spectre (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.