Jump to content
Larry Ullman's Book Forums

Converting Html To Bbc Code In A Smf Forum


Recommended Posts

I'm not a programmer, but manager several websites. I have a discussion forum for photographers and need to allow them to post their sales links from FineArtAmerica pages. For security reasons, forum members can't post full html code. Hence the existing FAA code needs to be converted to BBC Code for posting.

 

I've been working through this book and I can do the examples fine. But I'm having difficulty just isolating specific URLs to make strings that a PHP script can convert.

 

Basically I need to turn the first example code into the second:

 

//<a href=

'http://webaddress.html' size='20'><img src='webaddress2.html?id=5164335&width=250&height=166' alt='Photography Prints' title='Photography Prints' style='border: none;'</a>

 

http://webaddress.html][img alt=Photography Prints]http://webaddress2.html?id=5164335&width=250&height=166[/img]

 

with the second being inside a url tag.

 

I'm finishing Chapter 5 and trying to experiment, but so far can't isolate the urls. Any suggestions would be appreciated.

 - Greg

 

Link to comment
Share on other sites

Hello and welcome to the forums.

More than likely, you're going to want to use a regular expression to get what you want. Unfortunately, I don't fully understand what you want, so I can't give you a definitive answer.

 

Are you saying that you want to convert the following as follows?

 

<a href=
'http://webaddress.html' size='20'><img src='webaddress2.html?id=5164335&width=250&height=166' alt='Photography Prints' title='Photography Prints' style='border: none;'</a>
 
to
 
 
Please let us know, as I am confused.
Thanks.
Link to comment
Share on other sites

Thanks for responding. The url tags got removed in the second code.  Let's see if I can get this right with the code tag.  This first piece of code will post on a webpage or in a blog, but not in a discussion forum post.

<a href='http://greg-norrell.artistwebsites.com/featured/schwabacher-morning-light-greg-norrell.html' size='20'><img src='http://greg-norrell.artistwebsites.com/displayartworkartistwebsites.html?id=5164335&width=250&height=166' alt='Photography Prints' title='Photography Prints' style='border: none;'></a>

When converted to BBC Code it looks like this:

[url=http://greg-norrell.artistwebsites.com/featured/schwabacher-morning-light-greg-norrell.html][img alt='Photography Prints']http://greg-norrell.artistwebsites.com/displayartworkartistwebsites.html?id=5164335&width=250&height=166[/img][/url]

Some forum members can handle this on their own. Most cannot. So I'm working through this book and trying to write a PHP form that will allow my forum members to paste in their links in the form at top and the script return to them code they can copy in the form at bottom.

Link to comment
Share on other sites

Okay, I understand now.

As I suggested before, you're going to want to use a regular expression to capture the important bits of information in the string input by the users. You're then going to want to reorder those bits of information and add the additional string info as necessary.

 

I think this is best explained with some code, so let's look at the following:

 

<?php
  
  $input_str = '<a href=\'http://greg-norrell.artistwebsites.com/featured/schwabacher-morning-light-greg-norrell.html\' size=\'20\'><img src=\'http://greg-norrell.artistwebsites.com/displayartworkartistwebsites.html?id=5164335&width=250&height=166\' alt=\'Photography Prints\' title=\'Photography Prints\' style=\'border: none;\'></a>';
  
  preg_match('/<a href=\'(.*?)\'.*?><img src=\'(.*?)\' alt=\'(.*?)\'.*?><\/a>/', $input_str, $matches);
  
  echo '[url=' . $matches[1] . '][img alt=\'' . $matches[3] . '\']' . $matches[2] . '[/img][/url]';

 

I'm not going to explain how to post the form and whatnot. For the code above, please just assume that $input_str is equal to the string the user input.

 

Once you get the string, you need to use the preg_match function to run a regular expression on the input string and grab the following three pieces of information:

 

1) The a link href value

2) The img element src attribute value

3) The img element alt attribute value

 

While somewhat intimidating-looking, the regular expression can really be broken down as follows:

 

- \' is escaping the single quotes in your input str (since I used single quotes to mark the regular expression string as well). Something you should consider is what will happen if the user decides to use double quotes instead of single quotes for marking their HTML attribute values.

 

- The character combination .*?. A period in a regex will match any character except a newline character. The asterisk after the period says to match as many characters as possible, and the question mark after that tells the .* before it to not be greedy. In other words, match as little as you can. .*? is used many times in the regex to both capture the info we need, as can be seen by the .*? within parentheses, and simply to denote a bunch of characters that may or may not follow.

 

All the text captured by our regex capturing groups (the parentheses) is then stored in the $matches array. At that point, creating a new string in the format we want is a trivial task, as can be seen by the echo statement at the end of the code.

 

So just to recap, the main part of the code is the regex. If everything I said about regexes above makes no sense, then I would definitely study the regex chapter in Larry's book.

 

I hope that answers your question.

If you have any other questions, please ask.

Thanks.

  • Upvote 1
Link to comment
Share on other sites

Thank you HartleySan for this valuable input. I've gone through it tonight, and will try and work with it tomorrow. I'm most grateful for the help.

 

Edit: I have found Larry's book remarkably lucid for a technical book. I don't think I would have gotten where I did with any other book. I've just had a hard time putting the knowledge into practice. Hopefully I will do much better with that in the morning.

Link to comment
Share on other sites

Yes, Larry's books are very good at making all the technical stuff understandable.

And yes, I had the same problem as you at first. I got to the point where I could understand what Larry was doing and more or less copy his code, but stepping away from the code and using the techniques in the book to create something new was quite hard at first.

It all comes down to practice and time. Good luck.

Link to comment
Share on other sites

 Share

×
×
  • Create New...