Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Preg_replace Problems
galexcd
post Nov 18 2006, 05:17 AM
Post #1


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,074
Joined: 25-September 05
From: Los Angeles, California
Member No.: 12,251



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 [img] 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:

CODE
$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:
CODE
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%">';
else
return $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!
Go to the top of the page
 
+Quote Post
jlhaslip
post Nov 18 2006, 07:06 AM
Post #2


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,072
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



Check out the php function pathinfo and use it for capturing the extension.

http://us3.php.net/manual/en/function.pathinfo.php
Go to the top of the page
 
+Quote Post
galexcd
post Nov 18 2006, 08:59 PM
Post #3


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,074
Joined: 25-September 05
From: Los Angeles, California
Member No.: 12,251



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...
Go to the top of the page
 
+Quote Post
galexcd
post Nov 19 2006, 03:36 AM
Post #4


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,074
Joined: 25-September 05
From: Los Angeles, California
Member No.: 12,251



Bah, i replied before i tried it. It doesn't work. The function itself doesn't work with the input "$1"
Go to the top of the page
 
+Quote Post
jlhaslip
post Nov 19 2006, 05:45 AM
Post #5


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,072
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



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:
CODE

$extension = substr(strrchr($filename, "."), 1);
Go to the top of the page
 
+Quote Post
galexcd
post Nov 19 2006, 08:43 PM
Post #6


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,074
Joined: 25-September 05
From: Los Angeles, California
Member No.: 12,251



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 trap17's bbcode work?
Go to the top of the page
 
+Quote Post
peroim
post Dec 24 2006, 06:02 PM
Post #7


Newbie [Level 2]
**

Group: Members
Posts: 33
Joined: 9-July 06
From: Belgium
Member No.: 26,367



when I first saw this post, I was wondering why there were so many \ and / in
CODE
$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. biggrin.gif )

Thanks in advance,

Peroim
Go to the top of the page
 
+Quote Post
Spectre
post Jan 9 2007, 04:58 PM
Post #8


Privileged Member
*********

Group: Members
Posts: 873
Joined: 30-July 04
Member No.: 246



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)

CODE
$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.

This post has been edited by Spectre: Jan 9 2007, 05:01 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Replace Shtml With Php?(1)
  2. Problems With Apostrophes [ ' ] In Php(4)