galexcd
Nov 18 2006, 05:17 AM
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!
Reply
jlhaslip
Nov 18 2006, 07:06 AM
Check out the php function pathinfo and use it for capturing the extension. http://us3.php.net/manual/en/function.pathinfo.php
Reply
galexcd
Nov 18 2006, 08:59 PM
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...
Reply
galexcd
Nov 19 2006, 03:36 AM
Bah, i replied before i tried it. It doesn't work. The function itself doesn't work with the input "$1"
Reply
jlhaslip
Nov 19 2006, 05:45 AM
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);
Reply
galexcd
Nov 19 2006, 08:43 PM
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?
Reply
peroim
Dec 24 2006, 06:02 PM
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.  ) Thanks in advance, Peroim
Reply
Spectre
Jan 9 2007, 04:58 PM
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.
Reply
Recent Queries:--
preg_replace wildcard - 32.01 hr back. (1)
-
preg_match php detect non-alphanumeric characters - 45.36 hr back. (1)
-
preg replace on a file to preserver alignment - 95.20 hr back. (1)
-
preg replace apostrophe - 116.16 hr back. (1)
Similar Topics
Keywords : preg replace problems- A Simple Preg_replace Help Please.
- (1)
- Preg Replace Problem
- (1)
If i have a word like c.........a......t (with the dots in) and i wanted to replace it with cat how
would i do it. If i use CODE <?php $patterns =
"/c(*)a(*)t/"; $replace = "cat"; echo
preg_replace($patterns, $replace, 'c.....a.........t');
[/color][color="#000000"]?> then when i type something like "
c ome a nd look at t his" is also filtered. Is
there any way just to filter cat with dots in? ...
Php Preg Replace
- (1)
Ive got a problem with preg replace, this is the code: display.php CODE <table
border="1"> <tr><th width="70">Time</th><th
width="200">IP</th><th
width="70">Viewed</th></tr> <?php $LogData =
file_get_contents("log.txt"); $Find =
"/||(.*)|(.*)|(.*)||/i"; $Replace =
"<tr><td>$1</td><td>$2</td><td>$3
</td></tr>"; ...
Validation Script - Detecting Illegal Characters
- preg_match to find illegal characters (0)
I'm trying to validate a user's desired username for a registration page. I want to detect
any illegal characters being used for a username. So far i have managed to include most execpt the
'\', the '\' is used for escape and treating the preceding characters
literally. I try to do '\\' but i get an error because it execects another
character. CODE $ck_result = "Default"; $pattern =
"/[!|@|#|$|%|^|&|*|(|)|_|\-|=|+|\||,|.|\/|;|:|\'
;|\"|...
Php Problems
- Clan Web site Problem (3)
Okay, so I have an old version of Nettek that I would like to modify. There is a bug that when you
get to the rank required for Set Rank, you can set your rank or other people's ranks to what
ever you want, even Webmaster and that's something I'd like to not have. I am wondering how
to fix the problem. What would I modify in the coding to set have it so you can only move people up
to 1 rank below you at best? I'm still learning PHP and can't modify things like this quite
yet....
Include Funtion Php Problems
- in xammp and php 5.1.1 (11)
Hi, I just recently installed the latest versin of XAMMP on my computer which comes with php5.1.1
and i have been noticing some problems with the include(); funtion. This is a code that worked
fine on trap17 hosting so I am thinking it has something to do with the php version of 5.1.1 since
trap17 uses 4.3.X. Now what the actual problem is is using the variables in the include function.
If i have a normal include sentence like this one: CODE include "hello.php"; then it
works fine but as soon as i start using variables it somehow dosent work at all. ...
Problems With Php
- (5)
Recently, my system was infected by a Trojan Horse. Since then, I've been finding files missing
and now my PHP will not work, my Apache server will not display PHP files, i've tried getting
rid of the files in my C:\Windows and C:\Windows\system32 folders but nothing is
working. (it comes up with 'file in use') All help greatly appreciated....
Problems With Data Formatting
- (2)
I have a MySQL database which stores articles. A sample article would look like this: CODE This
is a body. This is a body.This is a body.This is a body.This is a body.This is a body.This is a
body.This is a body.This is a body.This is a body.This is a body.This is a body.This is a body.This
is a body.This is a body.This is a body.This is a body. This is a body.This is a body.This is a
body.This is a body.This is a body.This is a body.This is a body.This is a body.This is a body.This
is a body.This is a body.This is a body.This is a body.This is a body. That'...
Help With Preg_replace
- (3)
I must be a total noob, or just really tired...............-.- either way, I cannot understand what
exactly those funky strings preg_replace wants for the pattern. Could sombody explain to me exactly
what the pattern string and the replace string in preg replace does? I get str_replace...
that's quite basic. I considered myself a pro at php until tonight........haha shows how much i
know. I bet tomorow, if i look it up I'll get it.... Anyway.. any help? Thanks.....
/laugh.gif" style="vertical-align:middle" emoid=":lol:" border="0" alt="laugh.gif" />...
Replace A Character In A Specific Tag
- (7)
I need a code that replaces all spaces between ] brackets with an underscore. So, basically, I
write this into a variable: CODE bla bla something [[some text]] more text here
And I want a script to change it to this: CODE bla bla something [[some_text]]
more text here Any suggestions?...
Mysql Authentication Problems
- (11)
I installed the new version of both php and mysql on my computer and I am trying to work on a
database. The problem is the following. Even though I have the latest version of both php and mysql,
and I have created users in the new mysql version, I still get the problem that I get an error
message about authentication problems. I have no clue what I am doing wrong. It did work for a short
period of time, but somehow it is no longer working. Is there anyone who has a tutorial on how to
install both php and mysql? I have the feeling that there are a lot of settings in my.in...
Odbc Form Problems
- (0)
Hy everyone! I am moderately familiar
with PHP and have been using it for a couple years for various different things.
Basic information about my system:
PHP 5 MySQL 4.1 Apache Windows XP Pro I'm using
PHP/MySQL for a web-based server and have an ODBC set up in my office. I'm running into some
problems using the ODBC code as opposed to the MySQL code in some scripts I ha...
Phpnuke Newsletter Sending Problems
- (0)
i have a problem sending newsletters....when i send them no1 receives it...i tried sending to the
registered users on my site which im in and i didnt recieve it in my mail...i dont know why this is
happening...it says the newsletter is sent...but we get nothing in our mail... heres the code
QUOTE /************************************************************************/ /* PHP-NUKE:
Web Portal System */ /* ===========================
*/ /* ...
Regexp Function Preg_match_all()
- preg_match_all() - Help me (0)
Hi, I got a new problem which has caused me to go mad but no solution. preg_match_all() - is the
problem. I have something like this: CODE [ol] [li]Test1[/li]
[li]Test2[/li] [li]Test3[/li] [li]Test4[/li]
[li]Test5[/li] [/ol] Some text.Some text.Some text. [ol]
[li]Test1[/li] [ol] [li]Test1[/li]
[li]Test2[/li] [li]Test3[/li] [li]Test4[/li]
[li]Test5[/li] &...
Major Problems With Php Script
- Hold on for this one! (8)
Okay, so I got a php script for my site called php Bible. So I get it and go to install it, and
then the problems start. You see it uses php, dud, to search and view scripture. So here are the
instructions: QUOTE 1) Upload the files to a useful place inside your web server's
document tree. 2) Change the things that look wrong in config.php. 3) Make sure "AcceptPathInfo
ON" (or analogous) is somewhere in your PHP file section of your web server config. 4) Go over
steps 2 and 3 again, because you probably missed something for lack of documentation. So...
Problems With Php Form?
- Can you please help? (7)
I am trying to install a PHP form. I am using a tutorial I found in the tutorials section. It was
really good, but I have a few problems. Here is the link, if you need it:
http://www.trap17.com/forums/index.php?sho...ic=8118&hl=form Anyway, so I place the mailform.php
in an html document on file manager right? So I do that. And I put it in the body: CODE
<?php $Name = $_POST['Name']; $Subject =
$_POST['Subject']; $Email = $_POST['Email'];
$Site = $_POST['Sit...
Help With Reading Files
- Read and replace/insert data from form (5)
Hi, Does anyone know how i can do this, or scripts that will work on Trap17's servers and will
do the following: I have a .doc file form. Which i want to have filled in automactically, by HTML
Form and emailed to my address, with the data filled in. Any ideas? I have heard many differing
things, like XML, RTF, DOC, PDF... I have serched through many places and come up empty handed with
anything that works. /sad.gif' border='0' style='vertical-align:middle' alt='sad.gif' /> ...
<? Include ?> Problems
- (4)
Well, I'm new to PHP, and I get tons of problems when I use the include tags: Let's say my
site is www.mysite.com, and the menu is www.mysite.com/menu.htm. The site I want to include menu.htm
in is not in the same folder as menu.htm, but in "folder2": www.mysite.com/folder2/index.php The
images in menu.htm are located in it's own folder: www.mysite.com/images. However, when I load
index.php with , the images won't load. Then I try to check the image location and it says that
the images that didn't load was located in www.mysite.com/folder2/images ...
Browser Problems, Maybe?
- Firefox vs IE (6)
Alright, I coded something the other day for the game I am working on making, and a few of my
friends I allowed to get on it and test things out. Well....the ones that were on IE their
characters were made but the User variable as in which account they were on just stayed blank, while
with my friends that were on Firefox, everything worked fine and they could play perfectly... So I
was wondering, with PHP sql codes being sent to the database, is there something special you have to
do to make it work on both, or could it be a setting on the IE that was messing with it.....
Postnuke Install Problems On Subdomains
- (1)
I'm trying to install PostNuke on a empty server I allready got PostNuke downloaded and uploaded
the downloaded files to the server but I can't get it to work. I'm trying to do this on 2
servers both are sub-domains and both support php. One of them has a MySQL database and MyAdmin but
I can't find them anywhere on the sub-domain. I'm pretty new to this and any advise would be
more then welcome to me. I tryed looking for a MySQL database to install but when I do I get
something like 30 different versions and don't know whitch one I need I tryed th...
Php And Gd Tutorial
- I'm having problems (6)
I am trying to learn PHP and was wondering what is wrong with the tutorals on this site:
http://www.phpfreaks.com/tutorials/105/1.php I copied the code between the body tages in my site,
and am returned with an error regarding headers. Does anybody why this is?...
Problems With Apostrophes [ ' ] In Php
- (4)
Yes I have another problem. I have a blog that I made with php and mysql. It's working just
fine except for the text being displayed on the page. Everytime there's an apostrophe
encountered, *something puts a slash before it. It's fine in the database only the page.
Probably a problem with PHP. I hope you understand what I say. Here's a sample line displayed
in my blog: -- It\'s Saturday but I\'m still in the office. -- ...
Replace Shtml With Php?
- I don't know jack.... (1)
I don't know anything about PHP, except that I use phpBB and have modified my forums template
files. I used to make web sites years ago and just got back into it a couple months ago. I wonder
if I should stop using shtml and switch to php. I don't really have time to learn php (at least
not yet), but if it's easy to replace my Server Side Include with php (just to be more "modern")
I would be willing to do so. Anyone have any suggestions about me doing so? Am I'm too old
school for using shtml? Lol /laugh.gif' border='0' style='vertical-align:middle' ...
Ftp Script Problems - Authentication Failure
- (3)
I uploaded the code as a text file because it's pretty big: http://beeseven.trap17.com/ftp.txt
As I said it can't login. I get this error: Warning : ftp_login(): Authentication failed,
sorry in /home/beeseven/public_html/ftp.php on line 45 I think it might have something to do
with character encoding, but I'm not sure. If you think it is, what kind of encoding would work
as if I typed it directly? I tried putting it in the file, but then I got the missing required
fields error....
Mysql Relational Problems
- (3)
didn't know where to post this but since I build this database with php, I post it here. I have
to build a relational database 4 a school project. Made a webbased interface and made a database
with primary&foreign keys and not null fields to preserve the integrety of my database. -When I
insert an empty field or I just don't give the field where the field has the property NOT NULL
it just fills in nothing or some default value (it ignores the not null property). -When I insert 2
rows with the same primary key in the same table it doesn't throw an error but ...
Looking for preg, replace, problems
|
|
Searching Video's for preg, replace, problems
|
advertisement
|
|