Jump to content

Recommended Posts

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, '&#034') + 1); 

$c = substr($b, 0, strpos($b, '&#034')); 

echo $c;

Link to comment
https://forums.phpfreaks.com/topic/210387-extract-part-of-a-string/
Share on other sites

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

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

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.

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];

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

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

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 )

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);

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?

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];

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

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.

 

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?

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));

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];

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

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.