Reply
Strange preg_replace problems
Old 04-29-2008, 04:36 PM Strange preg_replace problems
Gilligan's Avatar
Dead Like Me

Posts: 1,572
Name: Stefan
Location: London, UK
Heres a snippet of my code

PHP Code:
$form $ecm2;
                        
        
$ecm2 preg_replace ('/\[title\](.*?)\[\/title\]/is''</p><h1>$1</h1><p>'$ecm2);
                        
        
$ecm2 preg_replace ('/\[b\](.*?)\[\/b\]/is''<strong>$1</strong>'$ecm2);
                        
        
$ecm2 preg_replace ('/\[i\](.*?)\[\/i\]/is''<em>$1</em>'$ecm2);
                        
        
$ecm2 preg_replace ('/\[u\](.*?)\[\/u\]/is''<u>$1</u>'$ecm2);
                        
        
$ecm2 preg_replace ('/\[img\](.*?)\[\/img\]/is''<img src="$1" alt="Image" />'$ecm2);
                        
         
$ecm2 preg_replace ('/\[url=(.*?)\](.*?)\[\/url\]/i''<a href="$1">$2</a>'$ecm2);
                        
         
$ecm2 htmlentities($ecm2); 
Kind of like BB Code if you want to think of it that way, but for some reason it won't replace the bb code with the HTML. It'll just display the BB Code as it is entered..

Eg:
Someone enters [ b ]stuff[ /b ]
it should replace it into <strong>stuff</strong>
but instead displays it as [ b ]stuff[ /b ]

Spaces in [ b ] are to avoid making this part of my post bold..lol

Can anyone see any problems with the code...Or something that might be causing this, and no..before you ask no php errors being displayed back.

Last edited by Gilligan : 04-29-2008 at 04:39 PM.
Gilligan is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-29-2008, 04:49 PM Re: Strange preg_replace problems
Skilled Talker

Posts: 92
Try using the preg_replace with the count variable, and see how many things it actually changes.

Ex:
PHP Code:
preg_replace ('/\[url=(.*?)\](.*?)\[\/url\]/i''<a href="$1">$2</a>'$ecm2, -1$count);
echo 
$count;
//-1 is for no limit on number of replacements 
</span></span>
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 04-30-2008, 03:48 PM Re: Strange preg_replace problems
Gilligan's Avatar
Dead Like Me

Posts: 1,572
Name: Stefan
Location: London, UK
It says 0..So obviously something is wrong

what I'm doing is making $form = $ecm, then replacing bb codes in $ecm. Then echoing $form.

Is that the problem??

Also, nl2br is NOT working also..

Here's a larger Snippet..

PHP Code:
$title $_POST['pgtitle'];
    
$heading $_POST['heading'];
    
$content $_POST['content'];
    
$content nl2br($content);
                        
    
$form $ecm2;
                        
    
$ecm2 preg_replace ('/\[title\](.*?)\[\/title\]/is''</p><h1>$1</h1><p>'$ecm2);
                        
    
$ecm2 preg_replace ('/\[b\](.*?)\[\/b\]/is''<strong>$1</strong>'$ecm2);
                        
    
$ecm2 preg_replace ('/\[i\](.*?)\[\/i\]/is''<em>$1</em>'$ecm2);
                        
    
$ecm2 preg_replace ('/\[u\](.*?)\[\/u\]/is''<u>$1</u>'$ecm2);
                        
    
$ecm2 preg_replace ('/\[img\](.*?)\[\/img\]/is''<img src="$1" alt="Image" />'$ecm2);
                        
    
$ecm2 preg_replace ('/\[url=(.*?)\](.*?)\[\/url\]/i''<a href="$1">$2</a>'$ecm2);
                        
    
$ecm2 htmlentities($ecm2);
                        
    echo 
'<form action="download.php" method="post"><textarea name="textarea" cols="80" rows="20">'.$form.'
                         </textarea><input type="hidden" name="filename" value="'
.$title.'">
                         <br><input type="submit" value="Download"></form>'


Last edited by Gilligan : 04-30-2008 at 03:57 PM.
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 04-30-2008, 06:12 PM Re: Strange preg_replace problems
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 603
If you're setting $form = $ecm2 at the beginning of your code, any changes you make to $ecm2 later won't be applied to $form. It's = $ecm2 for the value of $ecm2 at that particular line.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 05-01-2008, 03:17 AM Re: Strange preg_replace problems
Gilligan's Avatar
Dead Like Me

Posts: 1,572
Name: Stefan
Location: London, UK
So if I move $form = $ecm2 BELOW all the preg_replace()s it should work right?

Will try this later....
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-01-2008, 12:51 PM Re: Strange preg_replace problems
Gilligan's Avatar
Dead Like Me

Posts: 1,572
Name: Stefan
Location: London, UK
Okay, so the preg_replace is working now

But the nl2br now isn't ...am I doing that part right (see above code)
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-01-2008, 05:54 PM Re: Strange preg_replace problems
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 603
I'm not quite sure what it does. I'm assuming it converts new lines to breaks, but you'd actually have to post the function for me to tell you for sure. Also, I don't see that you do anything with $content after running it through nl2br(), though maybe you didn't post that part. If you're comfortable posting more and then explaining a little more about what you're doing, it might help.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 05-02-2008, 01:51 PM Re: Strange preg_replace problems
Gilligan's Avatar
Dead Like Me

Posts: 1,572
Name: Stefan
Location: London, UK
Its a template script, fill in a few boxes here and there and it feeds out a page written in html for the user to download.

Each client obviously has a different template, so i use if statements to determine which user it is.

$content is what the main box is that the user fills in, its a textarea.

$form = $ecm2
$ecm2 is contained in an external php page which has been included. Inside this file is something like

PHP Code:
<?php

$ecm2 
'<html>
stuff
goes
here

removed
'
.$content.'

other box variables here
more here 
etc
etc
etc'
;

?>
So thats where $content is used..inside $ecm2

nl2br = new line to break (<br>)
Converts new lines into html line breaks

Last edited by Gilligan : 05-02-2008 at 01:52 PM.
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-03-2008, 03:56 PM Re: Strange preg_replace problems
Gilligan's Avatar
Dead Like Me

Posts: 1,572
Name: Stefan
Location: London, UK
Anything wrong with it?
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-04-2008, 01:29 PM Re: Strange preg_replace problems
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 603
There isn't anything that I can tell, but maybe you could post the innards of the nl2br() function as well. Also, I can't tell from what you posted where you are including your file that contains $ecm2...that might make a difference depending on where you have it. As a side note, you might want to wrap up your BBcode replacement into an external function in case you want to reuse it later.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 05-05-2008, 08:14 AM Re: Strange preg_replace problems
Gilligan's Avatar
Dead Like Me

Posts: 1,572
Name: Stefan
Location: London, UK
including the file before the code starts..

innards of nl2br() ???

PHP Code:
$content nl2br($content); 

Last edited by Gilligan : 05-05-2008 at 08:18 AM.
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-05-2008, 11:45 AM Re: Strange preg_replace problems
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 603
Quote:
Originally Posted by Gilligan View Post
innards of nl2br() ???
Sorry, I wasn't familiar with that function. I thought it was one that you created yourself, but now I found it on the PHP website.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Reply     « Reply to Strange preg_replace problems
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16955 seconds with 13 queries