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
Global preg_replacing isn't working :(
Old 07-17-2010, 12:21 PM Global preg_replacing isn't working :(
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
I'm trying to make a BBCode parser, and so far I have it working well enough, but, well, not well enough.

For example, it can do this:
Bold
but it can't do this:
bold red
for some reason.

I'm using a preg_replace to match the BBCodes, but it doesn't work.

PHP Code:
$bbreplace = array (
"/(\[[Ii]\])(.+)(\[\/[Ii]\])/",
"/(\[[Bb]\])(.+)(\[\/[Bb]\])/",
"/(\[[Uu]\])(.+)(\[\/[Uu]\])/",
"/(\[[Ss]\])(.+)(\[\/[Ss]\])/",
"/(\[color=)(.+)(\])(.+)(\[\/color\])/",
"/(\[colour=)(.+)(\])(.+)(\[\/colour\])/",
"/(\[url=)(.+)(\])(.+)(\[\/url\])/",
"/(\[img\])(.+)(\[\/img\])/
"
);

$bbreplacements = array 
(
"<i>\\2</i>",
"<b>\\2</b>",
"<u>\\2</u>",
"<s>\\2</s>",
"<span style=\"color:\\2\">\\4</span>",
"<span style=\"color:\\2\">\\4</span>",
"<a href=\"\\2\">\\4</a>",
"<img src=\"\\2\" />
"
);
$string "[color=red][b]bold red[/b][/color]";
$newReplyContent preg_replace($bbreplace$bbreplacements$string); 
It will just echo it bold, but not red . Unfortunately, the above example works perfectly, but when I use it on my site, it suddenly craps out.

What is wrong with that code? I got it from the BBCode site and adapted it a bit, but that's it.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 07-17-2010 at 12:24 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-17-2010, 02:51 PM Re: Global preg_replacing isn't working :(
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
What does $newReplyContent come out like?
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-17-2010, 09:03 PM Re: Global preg_replacing isn't working :(
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Well it usually ends up echoing just the last line of the string (I'm dealing with large strings)

If you look at the source it's something really messed up like

<span style="color:red]something the stringbold[/<span>

Something weird like that. This is from mobile so I can't get the full report on it, but it's something along those lines. It's just not replacing ALL of the tags, just SOME of them, where and when it likes to :|

Other information:
I am running the BBCode off inside a config.php file, which is included inside the file I am calling the preg_replace function (thread.php).
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 07-18-2010, 02:52 PM Re: Global preg_replacing isn't working :(
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Once I removed the newlines in the last elements of the search and replaces arrays (from the last element), it worked.

(I had to upload as file, because for some reason I could not get it to display below)
Attached Files
File Type: txt example.txt (682 Bytes, 3 views)
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic; 07-18-2010 at 02:59 PM..
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-19-2010, 07:19 PM Re: Global preg_replacing isn't working :(
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Hmm, for some reason my computer can't download that... Can you maybe try Pastebin? Thanks
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 07-20-2010, 01:38 AM Re: Global preg_replacing isn't working :(
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Try: http://www.kmarshall.com/example.txt
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-20-2010, 05:38 PM Re: Global preg_replacing isn't working :(
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Great! That works amazingly! Thank you

(In case this thread is googled but the file is taken down, and somebody wants the answer, here is what the file contains: )


Code:
<?php


$bbreplace = array (
"/(\[[Ii]\])(.+)(\[\/[Ii]\])/",
"/(\[[Bb]\])(.+)(\[\/[Bb]\])/",
"/(\[[Uu]\])(.+)(\[\/[Uu]\])/",
"/(\[[Ss]\])(.+)(\[\/[Ss]\])/",
"/(\[color=)(.+)(\])(.+)(\[\/color\])/",
"/(\[colour=)(.+)(\])(.+)(\[\/colour\])/",
"/(\[url=)(.+)(\])(.+)(\[\/url\])/",
"/(\[img\])(.+)(\[\/img\])/");

$bbreplacements = array 
("<i>\\2</i>",
"<b>\\2</b>",
"<u>\\2</u>",
"<s>\\2</s>",
"<span style=\"color:\\2\">\\4</span>",
"<span style=\"color:\\2\">\\4</span>",
"<a href=\"\\2\">\\4</a>",
"<img src=\"\\2\" />");
$string = "[b][color=red]Bold Red[/color][/b]";
$newReplyContent = preg_replace($bbreplace, $bbreplacements, $string);  
echo htmlentities($newReplyContent);
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 07-20-2010 at 05:40 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 07-20-2010, 10:38 PM Re: Global preg_replacing isn't working :(
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I don't know why, but everytime I tried to paste that into here, it would break - Why would it work fine for you??
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-21-2010, 08:44 AM Re: Global preg_replacing isn't working :(
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
I put noparse tags around the code
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Global preg_replacing isn't working :(
 

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.29269 seconds with 13 queries