Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding quotes in a string

Status
Not open for further replies.

McKaulick

Programmer
Oct 24, 2002
35
0
0
AR
Hello,

I have a string coming from the database, and I want to verify if the string has a double quotes (") at the first position of the string and the last position of the string. I tried many things, but nothing works like this!

$sv['SEARCH_TEXT'] is a field from the database and has doubles quotes but return false when should be true !

if(stristr($sv['SEARCH_TEXT'], "\"") === TRUE) {
echo '"quotes" found in string';
}

Thanks for any help!


 
I think stristr() is not the appropriate function to use here becuase it's going to return a non-FALSE value if a doublequote is found anywhere in the string, not specifically at the beginning or end.

You must either explicitly check the first and last characters:

Code:
<?php

$a = '"The rain in Spain"';

if ($a[0] == '"' && $a[strlen($a) - 1] == '"')
{
	print 'good';
}
else
{
	print 'bad';
}
?>

or use a regular expression:

Code:
<?php

$a = '"The rain in Spain"';

$result = preg_match ('/^".*"$/', $a);

print $result;
?>

The former is preferrable because the online manual recommends not using regular expressions except where necessary. The first example exploits PHP's feature that a text string can be manipulated as an array of single characters.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top