Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Strange preg_replace problems
Old 04-29-2008, 05:36 PM Strange preg_replace problems
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE

Last edited by Gilligan; 04-29-2008 at 05:39 PM..
Gilligan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-29-2008, 05:49 PM Re: Strange preg_replace problems
Extreme Talker

Posts: 177
Trades: 0
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, 04:48 PM Re: Strange preg_replace problems
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
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>'

__________________

Please login or register to view this content. Registration is FREE

Last edited by Gilligan; 04-30-2008 at 04:57 PM..
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 04-30-2008, 07:12 PM Re: Strange preg_replace problems
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
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, 04:17 AM Re: Strange preg_replace problems
Gilligan's Avatar
Website Designer

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

Will try this later....
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-01-2008, 01:51 PM Re: Strange preg_replace problems
Gilligan's Avatar
Website Designer

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

But the nl2br now isn't ...am I doing that part right (see above code)
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-01-2008, 06:54 PM Re: Strange preg_replace problems
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
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, 02:51 PM Re: Strange preg_replace problems
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE

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

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
Anything wrong with it?
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-04-2008, 02:29 PM Re: Strange preg_replace problems
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
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, 09:14 AM Re: Strange preg_replace problems
Gilligan's Avatar
Website Designer

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

innards of nl2br() ???

PHP Code:
$content nl2br($content); 
__________________

Please login or register to view this content. Registration is FREE

Last edited by Gilligan; 05-05-2008 at 09:18 AM..
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-05-2008, 12:45 PM Re: Strange preg_replace problems
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.33975 seconds with 12 queries