Jump to content

Recommended Posts

I am getting lost on how to "extract" part of a variable's string leaving only what needs to be seen. I tried using explode function but that was not allowing it to save the tags around the text with it.

 

This is the paragraph im processing:

 

[24]Test[25]Thank you for reading![/quote]

Response 14455.

 

Now this whole paragraph above is in a variable as $Message

 

But i want to forward the message but to stop the user "editing" what is being quoted i want to some how extract the quote.

 

So from [24] to [/ QUOTE] it needs to take. So it then assigns it to a new variable so the new variable will become:

[24]Testing[25]Thank you for reading![/quote]

 

Leaving only for the main variable:

Response 14455. 

 

 

Can php do this? I know how to explode a string but explodes don't take the symbol that separates the information. I need this to also take the tags with it.

Link to comment
https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/
Share on other sites

Replace? I'm not trying to convert or replace anything how ever...im not sure if i have misunderstood you, or you have misunderstood me lol.

 

Just to sure, let me simplify it with a simple example say a string has:

 

Hello there!

 

And i wanted to "cut" this string so it become:

 

$Var1 = Hello

$Var2 = there!

 

 

Only for my real situation i need to cut the string "after" the last char of :

[/ QUOTE]

 

So "everything" after the ] char from [/ QUOTE]

Will go to $Var2 and everything before the ] include the ] itself goes to $Var1.

 

If i were to replace it I would loose tags. The tags need to remain otherwise when echo'd for later use it won't process in the BBCode.

You mean like this ?

 

<?php
$text = '[24]Test[25]Thank you for reading![/quote]';
if (preg_match('%(.*?\])([^]]*?\[\/QUOTE\])%si', $text, $regs))
{
$Var1 = $regs[1]; // [24]Test[25]
$Var2 = $regs[2]; // Thank you for reading![/quote]
}
?>

 

 

EDIT:

to be more exact, i guess you should have

if (preg_match('%(.*?\[25\])([^]]*?\[\/QUOTE\])%si', $text, $regs)) {

The RegEx matches everything until it hits a "[25]" and puts that into Var1, then matches everything until it hits a

and puts that into Var2

 

so, the results would be as follows (so it may not be what you want..

 

$text = '[24]Test[25]Thank you for reading!';

var1 = "[24]Test[25]"'

var2 = "Thank you for reading!"

 

$text = 'testing [24]Test[25]Thank you for reading!';

var1 = "testing [24]Test[25]"'

var2 = "Thank you for reading!"

 

$text = '[24]Test[25]Thank you for reading! can't find me';

var1 = "[24]Test[25]"'

var2 = "Thank you for reading!"

 

of course i could mod it to remove the [24] & [25] or only include the text inside them or almost anything but from what he said

So "everything" after the ] char from [/ QUOTE]

Will go to $Var2 and everything before the ] include the ] itself goes to $Var1.

 

i assume this is correct

if (preg_match('%(.*?\[25\])([^]]*?\[\/QUOTE\])%si', $text, $regs)) {

 

Can I just ask, MadTechie, what purpose do the % at the start and end serve? Like, obviously they are declaring the start and end of the string like ^ and $, but do they do anything better? :)

In RegEx you need to tag the start and end of the patten, after the last tag you can add switches

 

Now i am using 2 switches S and I, (dot match new lines and case insensitive), the tag i normall use are / but this can be almost anything I used % because of the / used in /QUOTE

I could of done this (escape the / in /quote)

if (preg_match('/(.*?\[25\])([^]]*?\[\//QUOTE\])/si', $text, $regs)) {

if i didn't escape the / then it would think i wanted the switches to be

"QUOTE\])/si" instead of just "si"

 

but i find it easier to use % then again i could use # or @ or : or ~ etc

ie for # it would be

if (preg_match('#(.*?\[25\])([^]]*?\[\/QUOTE\])#si', $text, $regs)) {

 

hope that makes sense :)

 

You mean like this ?

 

<?php
$text = '[24]Test[25]Thank you for reading![/quote]';
if (preg_match('%(.*?\])([^]]*?\[\/QUOTE\])%si', $text, $regs))
{
$Var1 = $regs[1]; // [24]Test[25]
$Var2 = $regs[2]; // Thank you for reading![/quote]
}
?>

 

 

EDIT:

to be more exact, i guess you should have

if (preg_match('%(.*?\[25\])([^]]*?\[\/QUOTE\])%si', $text, $regs)) {

 

Close to that but the two variables should be become like this:

 

	$Var1 = $regs[1]; // [24]Test[25] Thank you for reading![/quote]
$Var2 = $regs[2]; // Response 14455.

 

So everything before the last char of "

" which is the ] is in one var... and everything after the ] goes to the second var.

 

The original string is:

[24]Test[25]Thank you for reading![/quote]

Response 14455.

Try this (i'm at work so can't test it but looks ok)

<?php
$text = '[24]Test[25]Thank you for reading![/quote] Response 14455.';
if (preg_match('%^(.*?\[\/QUOTE\])(.*)$%si', $text, $regs)) {
{
$Var1 = $regs[1]; // "[24]Test[25]Thank you for reading![/quote]"
$Var2 = $regs[2]; // " Response 14455." 
}
?>

Thankyou for your response. It works a treat!

 

Although to cover all eventualities of a forum post.... it is very common for a message to contain:

 

<?php
$Message = $MainMessage = '[ quote] [ quote] test [/quote] why are you testing[/quote] because i want to!';
?>

 

They may even have more quotes than that. And so at this stage it wouldn't split the message correctly as i just tried it, it cuts the first end quote then nothing after that. Though I still require it to cut into two $vars only no matter how many [ /quote ] tags are invovled.

 

It should end up like:

<?php
$message[0] = '[ quote] [ quote] test [/quote] why are you testing[/quote]' // the quotes
$message[1] = 'because i want to!'  // the post

 

Any ideas?

 

Thanks for your response.

Okay little different style but again may work (can't test here)

 

<?php
$text = '[ quote] [ quote] test [/quote] why are you testing[/quote] because i want to!';
if (preg_match('%(.*\b\[/quote\])(.*)%si', $text, $regs)) {
{
$Var1 = $regs[1]; // "[ quote] [ quote] test [/quote] why are you testing[/quote]"
$Var2 = $regs[2]; // " because i want to!"
}
?>

Okay little different style but again may work (can't test here)

 

<?php
$text = '[ quote] [ quote] test [/quote] why are you testing[/quote] because i want to!';
if (preg_match('%(.*\b\[/quote\])(.*)%si', $text, $regs)) {
{
$Var1 = $regs[1]; // "[ quote] [ quote] test [/quote] why are you testing[/quote]"
$Var2 = $regs[2]; // " because i want to!"
}
?>

 

Thanks MadTechie. The if statement doesn't seem to come out as true though anymore:

 

<?php
$Message = $row['Message'];
//hiiide quote				
if (preg_match('%(.*\b\[/quote\])(.*)%si', $Message, $regs)) {
{
	$Var1 = $regs[1]; // "[ quote] [ quote] test [/quote] why are you testing[/quote]"
	$Var2 = $regs[2]; // " because i want to!"
}
}Else{
Echo 'failed';
}

 

The failed is being echo'd. Why could that be?

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.