AdRock Posted August 10, 2010 Share Posted August 10, 2010 I have string like this (without the spaces) [ QUOTE=Adrock]This is a test message[/quote ] How do i get just what is between the double quotes taking into account in the message there could be double quotes? I've tried this but it doesn't echo anything unless i put the square brackets in but i want the username $origauth = html_entity_decode($message); $b = substr($origauth, strpos($origauth, '"') + 1); $c = substr($b, 0, strpos($b, '"')); echo $c; Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/ Share on other sites More sharing options...
litebearer Posted August 10, 2010 Share Posted August 10, 2010 just a thought (sometimes I do have them)... Presuming there is only one set of double quotes in the string, simply EXPLODE the string into an array, using the double quote as the delimiter - the portion between the double quotes should be the second element of the array Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097832 Share on other sites More sharing options...
AdRock Posted August 10, 2010 Author Share Posted August 10, 2010 I did google this and found the example i gave and i did think about split/explode. If all users had the same length username it wouldn't be so hard so i can find where i need to start the extraction from and that would be the first set of double quotes, but i need to find out where the second set is Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097835 Share on other sites More sharing options...
freeloader Posted August 10, 2010 Share Posted August 10, 2010 So the quote is always presented in that format? quote="YOURTEXT" ? Why don't you just use a regex? $text = '[quote="Adrock"]This is a test message[/quote ]'; $a=preg_match("/QUOTE=\"(.*.)\"/",$text,$b); $searchText = $b[1]; echo "The text you're looking for is: ".$searchText; I haven't tested it, but that should do the job. Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097843 Share on other sites More sharing options...
litebearer Posted August 10, 2010 Share Posted August 10, 2010 As long as the first set of double quotes always contains the user name, it doesn't matter ohw long th euser name is OR if there are other double quotes in the message. All elements of the array AFTER the 2nd are irrelevant. example $some_string='[ QUOTE="Adrock"]"This is a test message.", said Sam.[/quote ]' $some_array = explode('"', $some_string); /* note the double quote is surrounding by single quotes $user_name = $some_array[1]; Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097845 Share on other sites More sharing options...
AdRock Posted August 10, 2010 Author Share Posted August 10, 2010 freeloader, your example worked if there were only 2 sets of quotes....thanks anyway litebearer, spot on....does what i want. This has been doing my head in all afternoon Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097851 Share on other sites More sharing options...
freeloader Posted August 10, 2010 Share Posted August 10, 2010 Could do a preg_match_all in that case, would catch all of them too. Glad you got it figured out anyway Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097852 Share on other sites More sharing options...
AdRock Posted August 10, 2010 Author Share Posted August 10, 2010 When testing that code with a hard coded string it worked but when trying it in my page it returns NULL I use html_entity_decode to get the quotes back etc and var_dumping that shows the whole string Trying to explode that doesn't now work with this code $original = html_entity_decode($message); //original string $myarray = explode('"', $original); $user_name = $myarray[1]; var_dump($myarray[1]); //prints NULL var_dump($user_name); //prints NULL probably something really obvious but i can't see it Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097854 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 been doing some debugging and found that it is not exploding. Echoing out $myarray[1] prints nothing Echoing out $myarray[0] prints the whole string I've tested it locally and it works, hosted doesn't work....could there be a problem if using differnet versions of PHP Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097862 Share on other sites More sharing options...
freeloader Posted August 11, 2010 Share Posted August 11, 2010 Can you post part of your input? Problem might be there. Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097868 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 This is the original message as stroed in database before html_entity_decode var_dump($message); string(78) "[ QUOTE=AdRock]just testing the error[/quote ] Another test" Then after decoding it but should make no difference $mess = html_entity_decode($message); $some_array = explode('"', $mess); print_r($some_array) Array ( [0] => [ QUOTE=AdRock]just testing the error[/quote ] Another test ) Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097872 Share on other sites More sharing options...
freeloader Posted August 11, 2010 Share Posted August 11, 2010 Interesting. I'd still do this with RegEx though, as that is most powerful in these situations. Why was it again you couldn't use RegEx here? Could always do it like this if the " are being a problem: $a=preg_match_all("/\[quote=(.*.)\]/",$text,$b); print_r($b); Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097875 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 not really hot on RegEx ad preg_match I've done what you suggested and this is the output Array ( [0] => Array ( ) [1] => Array ( ) ) $text = html_entity_decode($message); $a=preg_match_all("/\[quote=(.*.)\]/",$text,$b); print_r($b); What is $b? Also can this be used to extract the actual message too? Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097883 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 I've managed to get some ouptput combining your earlier post and last post I get the username but in quotes. How do i get rid of the quotes? The regex? Also, i tried it on another string but this time the output is different. [ QUOTE=AdRock]just testing the error[/quote ] gives me "AdRock"]just testing the error[/quote and (note that this is on 2 lines with line breaks) [ QUOTE=AdRock]blah blah blah blah blah blah blah blah blah blah[/quote ] gives me AdRock "AdRock" $text = html_entity_decode($message); $a=preg_match_all("/\[quote=(.*.)\]/",$text,$b); $searchText = $b[1]; echo $searchText[0]; Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097894 Share on other sites More sharing options...
sasa Posted August 11, 2010 Share Posted August 11, 2010 try <?php $test = '[quote="AdRock"]just testing the error[/quote] Another test'; $test = html_entity_decode($test, ENT_QUOTES); preg_match_all('/\[\s*QUOTE="([^"]*)"/s', $test, $out); print_r($out[1]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097945 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 I've tried what you said and by hard coding the string it works but as soon as i grab a string from the database which is exactly the same it prints and empty array. There is nothing wrong with any code that i can see, it just makes no sense. It's like it's refusing to add it to an array Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1097987 Share on other sites More sharing options...
litebearer Posted August 11, 2010 Share Posted August 11, 2010 as a test put the hard coded string into one variable and put the database generated string into a second variable. Then compare the two strings. you can use str_split() to: (1) determine total characters in the strings [it helps to see 'invisible' characters ie extra spaces]; (2) do a character by character comparison. Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1098011 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 what is really strange is that i was able to extract the rest of the string after the closing ] and print it out, so everyythignafter was printed. I don't know if it's to do with the quotes. I'll give your suggestion a try and let you know Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1098017 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 I've done this $test = '[quote="AdRock"]just testing the error[/quote] Another test'; $message = html_entity_decode($message, ENT_QUOTES); // from database $test = html_entity_decode($test, ENT_QUOTES); // hard coded string //preg_match_all('/\[\s*QUOTE="([^"]*)"/s', $message, $out); //print_r($out[1]); var_dump($message); var_dump($test); this is the output (without the extra spaces after square brackets) string(70) "[ QUOTE=AdRock]just testing the error[/quote ] Another test" string(59) "[ QUOTE=AdRock]just testing the error[/quote ] Another test" so what is the extra 11 chars? Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1098018 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 just checked using strlen() and the string from the database is 70 whereas the hard coded string is 69 Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1098028 Share on other sites More sharing options...
litebearer Posted August 11, 2010 Share Posted August 11, 2010 IF you create the two arrays using str_split(), you can use print_r to see what each element contains AND as such where the differences lay. Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1098031 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 Donw what you said and it appears that it is not doind the decode Array ( [ 0] => [ [1] => Q [2] => U [3] => O [4] => T [5] => E [6] => = [7] => & [8] => q [9] => u [10] => o [11] => t [12] => ; [13] => A [14] => d [15] => R [16] => o [17] => c [18] => k [19] => & [20] => q [21] => u [22] => o [23] => t [24] => ; [25] => ] [26] => j [27] => u [28] => s [29] => t [30] => [31] => t [32] => e [33] => s [34] => t [35] => i [36] => n [37] => g [38] => [39] => t [40] => h [41] => e [42] => [43] => e [44] => r [45] => r [46] => o [47] => r [48] => [ [49] => / [50] => Q [51] => U [52] => O [53] => T [54] => E [55] => ] [56] => [57] => [58] => A [59] => n [60] => o [61] => t [62] => h [63] => e [64] => r [65] => [66] => t [67] => e [68] => s [69] => t ) Array ( [ 0] => [ [1] => Q [2] => U [3] => O [4] => T [5] => E [6] => = [7] => " [8] => A [9] => d [10] => R [11] => o [12] => c [13] => k [14] => " [15] => ] [16] => j [17] => u [18] => s [19] => t [20] => [21] => t [22] => e [23] => s [24] => t [25] => i [26] => n [27] => g [28] => [29] => t [30] => h [31] => e [32] => [33] => e [34] => r [35] => r [36] => o [37] => r [38] => [ [39] => / [40] => Q [41] => U [42] => O [43] => T [44] => E [45] => ] [46] => [47] => A [48] => n [49] => o [50] => t [51] => h [52] => e [53] => r [54] => [55] => t [56] => e [57] => s [58] => t ) $test = '[quote="AdRock"]just testing the error[/quote] Another test'; $message = html_entity_decode($message, ENT_QUOTES); $test = html_entity_decode($test, ENT_QUOTES); print_r(str_split($message)); print_r(str_split($test)); Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1098040 Share on other sites More sharing options...
litebearer Posted August 11, 2010 Share Posted August 11, 2010 try this... $needle ="""; $string = '[quote="AdRock"]just testing the error[/quote] Another test'; $new_value='"'; $string = str_replace($needle, $new_value, $string); $new_array = explode($new_value, $string); echo $new_array[1]; Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1098048 Share on other sites More sharing options...
AdRock Posted August 11, 2010 Author Share Posted August 11, 2010 Did what you suggested and still wouldn't work so looked at what is stored in database and it seems that htmlentities stored the quote as &" so that is what the extra chars are and why it wasn't working. It works now so thanks very much chaps as this was doing more swede in for a whole day and night....ahhh Quote Link to comment https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/#findComment-1098054 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.