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
https://forums.phpfreaks.com/topic/165917-preg_match/
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
https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875150
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
https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875154
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
https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875180
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
https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875202
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
https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875523
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
https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-879613
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.