Page 1 of 1
Parsing text...2!
Posted: Tue Jul 09, 2002 9:23 am
by fatalcure
Thanks for the help b4 with parsing URL and Emails in posts.
How would I go about doing something similar to this forum, with the
, , , [quote], [img], [color], and [size]
Those are the things I want on a small forum Im creating myself, help would be appriciated, thanks 
Posted: Tue Jul 09, 2002 11:14 am
by BDKR
Hey,
How's this?
Code: Select all
$test_message = "їb] more testing ї/b]";
$new_message = preg_replace("/\їb](.*)\ї\/b]/", "MORE TESTING", $test_message);
My testing works. In your case, you would need to tweak the above code to add the things you need to see based on what you are searching for.
Also check out some links on regular expressions. A good bit of voodoo, but it's powerful stuff.
http://www.google.com/search?q=regular%20expressions
Hope this helps,
BDKR (TRC)
Posted: Tue Jul 09, 2002 11:24 am
by BDKR
As an addendum:
Another thing that can be can be done in some cases (once again, depending on what the tag is that you are searching for) is just simply use str_replace().
Code: Select all
$message = "їI] That's Mine! ї/I]";
$message = str_replace("їI]", ",<I>", $message);
$message = str_replace("ї/I]", "</I>", $message);
I'm sure you can figure out what's goin' on here.
Later on,
BDKR (TRC)
Posted: Wed Jul 10, 2002 1:05 pm
by fatalcure
well, yes, that would work, but what if somone enteres withought the ending /b]
it would bold everything
i only want to bold things between the and the ending /b]
Posted: Wed Jul 10, 2002 4:15 pm
by BDKR
Don't change the game now! You asked
how, not what if?
Now what's this?
i only want to bold things between the and the ending /b]
It occurs to me that the both of those responses answered the question. It's just a matter of what you want "" or anything else to represent.
Check google for tutorials on parsing files and stuff.
Rock hard, ride free!,
BDKR (TRC)
Posted: Thu Jul 11, 2002 3:51 am
by mikeq
fatalcure wrote:well, yes, that would work, but what if somone enteres withought the ending /b]
it would bold everything
i only want to bold things between the and the ending /b]
What ending /b]? you said what if they they didn't add the ending /b] so how do you know where it should be or where they intended it to go, does your program have ESP.
Mike
Posted: Thu Jul 11, 2002 2:46 pm
by fatalcure
uhm, BDKR said:
Code: Select all
$message = "їI] That's Mine! ї/I]";
$message = str_replace("їI]", ",<I>", $message);
$message = str_replace("ї/I]", "</I>", $message);
It will replace all the [X] with <x> alright, but if the person who is writing the message doesn't put the ending [/X] it'll italisize everything in the post...since str_replace("[/I]", "</I>", $message) will never happen.
get it??
so i ONLY want to bold, italic, etc things between [X] and [/X]
* im using [X] as an example of other values
Posted: Thu Jul 11, 2002 3:25 pm
by hob_goblin
you're not making sense.
Posted: Thu Jul 11, 2002 3:43 pm
by llimllib
yes he is. What he wants to do is assure himself that all opens are closed. To do this, fatalcure, what you will need is a loop that detects the opening (with strstr or strpos, for example) and searches for the closing. If it's found, do the replace, if not, don't, and you won't have the problem you're talking about.
Posted: Thu Jul 11, 2002 3:43 pm
by RandomEngy
fatalcure, what's wrong with that? What happens in HTML when you do this?
<html><body>
<b>I really like ham
</body>
</html>
That's right, it bolds the whole thing, because it assumes that the user just forgot the </b>. Think of that aspect of BDKR's example as error-checking.
If you REALLY want to have that however, try:
$message= "[b] more testing [/b] i like ham [b] wheee [/b]";
$message = preg_replace("/\[[Bb]\]([^\[]+)\[\/[Bb]\]/", "<b>\\1</b>", $message);
$message = preg_replace("/\[[Bb]\]/", "", $message);
echo $message;
This will convert to bold everything between [b] or [B] and [/b] or [/B]. Also, if there is no end [/b] tag for a [b] tag, it will get rid of the lone [b] tag. The only restraint is that you can't nest tags, because it will stop matching at the first instance of a [ .
Edit: Gah, you can't have it let you use [syntax=php]tags AND not eat the [b]'s .[/syntax]
Posted: Thu Jul 11, 2002 3:45 pm
by llimllib
yup, that works too.
Posted: Thu Jul 11, 2002 8:03 pm
by gnu2php
Here's some code that might be useful for you. If nothing else, it'll give you something to go on.
Code: Select all
<?
// All tags (these must be lowercase and include closing tags)
$valid_tags = array('b', 'i', 'u', '/b', '/i', '/u', 'img');
// Tags that require a closing tag </tag>
$open_tags = array('b', 'i', 'u');
print "Message #1<br>\n";
print check_tags("This is їb]my їi]їu]messї/i]age.")."<br>\n";
print "Message #3<br>\n";
/*
Output:
Message #1<br>
This is <b>my <i><u>mess</u></i>age.</b><br>
Message #3<br>
*/
function check_tags($data)
{
// Split tags and non-tags into an array
$data_array = preg_split('/(\її^\]]+\])/', $data, -1, PREG_SPLIT_DELIM_CAPTURE);
$tag_stack = array(); // List of tags waiting for </tag>
$out = ''; // The output
foreach ($data_array as $item)
{
// Get tag name (such as 'img' or '/b'), then see if it's valid
if (!preg_match('/^\ї((\/|)(їa-z]+))(ї^\]]+|)/i', $item, $matches) || !in_array(strtolower($matchesї1]), $GLOBALS{'valid_tags'}))
{
$out .= $item; // The tag is invalid, so we'll output it
continue;
}
$fullname = $matchesї1]; // Name of tag with '/' (if any)
$tagname = $matchesї3]; // Name of tag without '/'
$tag_data = $matchesї4]; // Remainder of tag (after the name)
if ($fullname != $tagname) // A closing tag </tag>
{
$out .= sync_tags($tag_stack, $tagname);
continue;
}
$out .= "<$fullname$tag_data>"; // Output the tag and its data
if (in_array(strtolower($tagname), $GLOBALS{'open_tags'}))
{
array_unshift($tag_stack, $tagname); // Add tag to stack
}
else
{
// ... do something with the їimg] tag, etc.
}
}
// Output closing tags that were never printed
foreach ($tag_stack as $item) $out .= "</$item>";
return $out;
}
// Makes sure the tags are nested correctly
function sync_tags(&$tag_stack, $current_tag)
{
$new_stack = $tag_stack; // Copy the stack in case $current_tag isn't found
$next_output = '';
foreach ($tag_stack as $item)
{
array_shift($new_stack); // Remove next tag from stack
$next_output .= "</$item>"; // Close the tag that was just on the stack
if (strtolower($item) == strtolower($current_tag))
{
$tag_stack = $new_stack; // Update the old stack
// Output the closing tags that will keep us in sync
return $next_output;
}
}
// If $current_tag isn't in the stack, we'll do nothing
}
?>
Posted: Fri Jul 12, 2002 8:28 am
by BDKR
Hey all,
You know, all the time spent squawking over whether or not I got it (and I did by the way) could've been used by Fatalcure to figure it out
The truth is that a lot of us just keep posting and never look for a clue or a hint on how to do things. I gave some help, but also posted a link where the person could get more information. Did the person bother to look for information on how to make sure that a person remembered to close a tag, or did he come straight here and post hoping someone would give him the answer?
Many of these things don't really take too much time to figure out. I can understand that there are little things in it that can give someone fits and seeking help in those areas is fine, but
EXPECTING someone to give you the code, or even
EXPECTING anything from people who are here to help is just flat wrong.
However, I normally try not to get hot whenever something rubs me wrong way. Fatalcure managed to rub me the wrong way so I responded with a bit of heat. My mistake
and my apology to make. I apologize.
But Fatalcure, you asked one question, got an answer, then changed up mid-stride as if I should've known to answer the next question. Do you see how this could've tweaked someone?
So seriously, do as much as you can to figure out something before asking for the answer. That makes you a much stronger developer over time. There is no way to debate that!
Rock hard, ride free!
BDKR (TRC)
Posted: Fri Jul 12, 2002 9:51 am
by fatalcure
haha, you think ppl are posting here squaking whethere you got it or not??
its funny how a lil question can steam you up, chill out buddy, this is a help forum, and regular expressions are not my thing, nor do I use them very often, so I came here to ask for help.
But Fatalcure, you asked one question, got an answer, then changed up mid-stride as if I should've known to answer the next question. Do you see how this could've tweaked someone?
I didn't change my mind about the question, I presented another aspect of it, if that got you mad, then your just a hot tempered person.
I do come to a help forum, especially one this big, and
EXPECT to get help, since there a lot of coders out there that know more than I.
Don't take this post personally, I wasn't attacking the reply you gave me, I appricated it...but anyway
Hope you cool down, walking around with a hot head isnt good

Posted: Fri Jul 12, 2002 10:28 am
by BDKR
Hope you cool down, walking around with a hot head isnt good
Yeah! I'm on fire
It's all good!
Later on,
BDKR