EchoFool Posted July 19, 2008 Share Posted July 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/ Share on other sites More sharing options...
DeanWhitehouse Posted July 19, 2008 Share Posted July 19, 2008 are u looking for bbcode? Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594479 Share on other sites More sharing options...
EchoFool Posted July 19, 2008 Author Share Posted July 19, 2008 No no. Just to break the string apart From the the first char to the last char of Then to the end of the string. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594481 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 i am pretty sure that is kinda bbcode will the start and end tags always be [24] Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594494 Share on other sites More sharing options...
EchoFool Posted July 20, 2008 Author Share Posted July 20, 2008 Yeh the tags will be the same every time. I just need to split that paragraph into two variables. Rather than one. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594501 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 okay then you will need a regex, using preg_replace and then you can convert [24] to anything and this will save the result in a string. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594504 Share on other sites More sharing options...
EchoFool Posted July 20, 2008 Author Share Posted July 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594517 Share on other sites More sharing options...
MadTechie Posted July 20, 2008 Share Posted July 20, 2008 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)) { Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594522 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 i didn't know about preg match, i was suggesting replacing the [24] and with just a blank space mad techie has a better solution Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594529 Share on other sites More sharing options...
MadTechie Posted July 20, 2008 Share Posted July 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594534 Share on other sites More sharing options...
JasonLewis Posted July 20, 2008 Share Posted July 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594539 Share on other sites More sharing options...
MadTechie Posted July 20, 2008 Share Posted July 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594546 Share on other sites More sharing options...
JasonLewis Posted July 20, 2008 Share Posted July 20, 2008 Yeah I know regular expressions. Was just wondering what the difference between the % and / were. Because I usually use / Cheers, I just thought it did something differently. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594548 Share on other sites More sharing options...
MadTechie Posted July 20, 2008 Share Posted July 20, 2008 Nope, they do the same thing just saves time when you have / in the patten Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594550 Share on other sites More sharing options...
JasonLewis Posted July 20, 2008 Share Posted July 20, 2008 Haha alright then. Shall remember. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594554 Share on other sites More sharing options...
EchoFool Posted July 20, 2008 Author Share Posted July 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594804 Share on other sites More sharing options...
EchoFool Posted July 20, 2008 Author Share Posted July 20, 2008 Bump Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-594931 Share on other sites More sharing options...
MadTechie Posted July 21, 2008 Share Posted July 21, 2008 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." } ?> Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-595355 Share on other sites More sharing options...
EchoFool Posted July 21, 2008 Author Share Posted July 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-595460 Share on other sites More sharing options...
MadTechie Posted July 21, 2008 Share Posted July 21, 2008 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!" } ?> Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-595482 Share on other sites More sharing options...
EchoFool Posted July 21, 2008 Author Share Posted July 21, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-595501 Share on other sites More sharing options...
MadTechie Posted July 21, 2008 Share Posted July 21, 2008 ooops, remove the \b (shouldn't be thier) if (preg_match('%(.*\[/quote\])(.*)$%si', $text, $regs)) { $Var1 = $regs[1]; $Var2 = $regs[2]; }else{ //No quotes found } Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-595520 Share on other sites More sharing options...
EchoFool Posted July 21, 2008 Author Share Posted July 21, 2008 works a treat! Thankyou ! Quote Link to comment https://forums.phpfreaks.com/topic/115645-solved-help-with-string-extraction/#findComment-595550 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.