Jump to content

preg_match


Chaos23

Recommended Posts

Basically I want to convert this:

<!--quoteo(post={PID}:date={DATE}:name={NAME})-->
<div class='quotetop'>QUOTE({USER} @ {DATE}) <a href="index.php?act=findpost&pid={PID}"><img src='snapback.gif' alt='*' border='0' /></a></div>
<div class='quotemain'><!--quotec-->{POST}<!--QuoteEnd--></div>
<!--QuoteEEnd-->

to this:

<div class='quotemain'>
Quote: {USER} @ {DATE} <a href="index.php?act=findpost&pid={PID}"><img src="snapback.gif" alt='*' border='0' /></a>
<hr />
{POST}
</div>

I didn't know wether preg_replace, or ereg_replace would be better, all the regex stuff is still confusing to me.

Link to comment
Share on other sites

I'm assuming {PID}, {DATE}, etc. Will be switched for actual data? Could we see a few examples? At the moment can't really give an accurate enough solution due to too many possibilities, well you could but adding in real data could break it. For example what format is the date? Can "user" contain spaces? etc.

Link to comment
Share on other sites

Yeah, I didn't think about that. Here is an actual example. I just need to pull out the {PID}, {DATE}, {NAME} & {POST}, so that I can do a DB query and re enter the data into the new format, I am using a DB query to determine what group a member is in, cause certain groups have different color posts, and I want the quotes to match the original posters colors.

<!--quoteo(post=279:date=Nov 26 2008, 09&#58;38 PM:name=Chaos23)-->
<div class='quotetop'>QUOTE(Chaos23 &#064; Nov 26 2008, 09&#58;38 PM) <a href="index.php?act=findpost&pid=279"><img src='images/snapback.gif' alt='*' border='0' /></a></div>
<div class='quotemain'><!--quotec-->I didn&#39;t copy it I quoted it<!--QuoteEnd--></div>
<!--QuoteEEnd-->

Link to comment
Share on other sites

Why don't you store the data in seperate fields, to prevent future issues like this? You can just format the post when you return it that way.

 

To make things a little simpler I returned the PID, DATE & NAME together then the POST separately:

 

<?php

$str ='<!--quoteo(post=279:date=Nov 26 2008, 09&#58;38 PM:name=Chaos23)-->
   <div class=\'quotetop\'>QUOTE(Chaos23 &#064; Nov 26 2008, 09&#58;38 PM) <a href="index.php?act=findpost&pid=279"><img src=\'images/snapback.gif\' alt=\'*\' border=\'0\' /></a></div>
   <div class=\'quotemain\'><!--quotec-->I didn&#39;t copy it I quoted it<!--QuoteEnd--></div>
<!--QuoteEEnd-->';

preg_match('#<!--quoteo\(post=([^:]+):date=([^:]+):name=([^\)]+)\)-->#', $str, $details);
preg_match('#<!--quotec-->(.+?)<!--QuoteEnd-->#', $str, $message);

print_r($details);
print_r($message);

?>

 

Should think you'll be okay from here?

Link to comment
Share on other sites

Thanks, That works perfectly. There is one other thing that I ran into. If a post has more than one quote in it uses the details from the first quote on both, what string could I use to kill the first one or could I use while or foreach to make each quote seperate.

Link to comment
Share on other sites

This is with more than one quote in a single post.

<!--quoteo(post=279:date=Nov 26 2008, 09&#58;38 PM:name=Chaos23)-->
<div class='quotetop'>QUOTE(Chaos23 &#064; Nov 26 2008, 09&#58;38 PM) <a href="index.php?act=findpost&pid=279"><img src='images/post_snapback.gif' alt='*' border='0' /></a></div>
<div class='quotemain'><!--quotec-->I didn&#39;t copy it I quoted it.<!--QuoteEnd--></div>
<!--QuoteEEnd-->
<br />
What I meant was copy/paste the text.
<br />
<!--quoteo(post=276:date=Nov 26 2008, 09&#58;02 PM:name=Chaos23)-->
<div class='quotetop'>QUOTE(Chaos23 &#064; Nov 26 2008, 09&#58;02 PM) <a href="index.php?act=findpost&pid=276"><img src='images/post_snapback.gif' alt='*' border='0' /></a></div>
<div class='quotemain'><!--quotec-->If he returns the project really should be wholely his and he can keep or abandon any changes made.<!--QuoteEnd--></div>
<!--QuoteEEnd-->
<br />
At the time I first read that I didn&#39;t know you were quoting it.

Link to comment
Share on other sites

If you replace the preg_match() functions with preg_match_all() and take a look at the difference in the data returned, you should be able to spot how you can use it for 2, 3, 4, etc. different quotes in a single post.

Link to comment
Share on other sites

Here is the entire function something is missing that makes the quotes repeat themselves.

function get_quote($post)
{
	global $tcn, $DB, $std, $print;

	preg_match('#<!--quoteo\(post=([^:]+):date=([^:]+):name=([^\)]+)\)-->#', $post, $details);
	preg_match('#<!--quotec-->(.+?)<!--QuoteEnd-->#', $post, $message);

	$quote_top = $details[3].' @ '.$details[2];

	if( $quote_top == ' @ ')
	{
		$quote_top = 'Quote:';
	} else {
		$quote_top = 'Quote: '.$quote_top;
	}

	$post = preg_replace('#<!--quoteo(.+?)-->(.+?)<!--QuoteEnd-->#', '<div class=\'quotemain\'>'.$quote_top.'<hr />'.$message[0], $post);

	return $post;
}

Link to comment
Share on other sites

This is the function with preg_match_all:

   function get_quote($post)
   {
      global $tcn, $DB, $std, $print;

      	preg_match_all('#<!--quoteo\(post=([^:]+):date=([^:]+):name=([^\)]+)\)-->#', $post, $details);
	preg_match_all('#<!--quotec-->(.+?)<!--QuoteEnd-->#', $post, $message);

	for ($i=0; $i < count($message[0]); $i++)
	{
		$post = str_replace('<!--quotec-->', '', $post);
		$post = str_replace('<!--QuoteEnd-->', '', $post);

		$quote_top = $details[3][$i].' @ '.$details[2][$i];

		if( $quote_top == ' @ ')
		{
			$quote_top = 'Quote:';
		} else {
			$quote_top = 'Quote: '.$quote_top;
		}

		$post = preg_replace('#<!--quoteo(.+?)-->(.+?)<!--QuoteEEnd-->#', '<div class=\'quotemain\'>'.$quote_top.'<hr />'.$message[0][$i].'</div>', $post);
	}

      return $post;
   }

 

This function works with individual quotes, or ones with only one snapback like this:

<!--quoteo(post=279:date=Nov 26 2008, 09&#58;38 PM:name=Chaos23)-->
   <div class='quotetop'>QUOTE(Chaos23 &#064; Nov 26 2008, 09&#58;38 PM) <a href="index.php?act=findpost&pid=279"><img src='images/post_snapback.gif' alt='*' border='0' /></a></div>
   <div class='quotemain'><!--quotec-->I didn&#39;t copy it I quoted it.<!--QuoteEnd--></div>
<!--QuoteEEnd-->
<br />
What I meant was copy/paste the text.
<br />
<!--quoteo-->
   <div class='quotetop'>QUOTE</div>
   <div class='quotemain'><!--quotec-->If he returns the project really should be wholely his and he can keep or abandon any changes made.<!--QuoteEnd--></div>
<!--QuoteEEnd-->
<br />
At the time I first read that I didn&#39;t know you were quoting it.

But not ones with multiple snapbacks.

All posts that have more than one.

<!--quoteo(post={PID}:date={DATE}:name={USER})-->

will repeat both the details and message throughout the main post that the quotes are contained in.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.