By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

How to find a quote in a string

P: n/a
I have a text file. My text file contains lines with double quotes
around it. I trying to code if a line has double quotes around it Then
make it bold

My code is below and it is not working. Please Help
<?

$filename = "whatsnew.txt"; # file to read

if (file_exists($filename)) {

# Get a handle to the asdfile
$file = file($filename);
echo "<p style='font-family:Arial; font-size:x-small'>";
foreach($file as $line) {
if (strpos($line,"\"") 0) {

$line = "<b>" . $line . "</b>";
}
$line .= "<br />";
echo $line;
}
echo "</p>";
}
?>
Oct 23 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
*** WebArchitect escribió/wrote (Thu, 23 Oct 2008 09:53:22 -0700 (PDT)):
I have a text file. My text file contains lines with double quotes
around it. I trying to code if a line has double quotes around it Then
make it bold

My code is below and it is not working. Please Help
<?

$filename = "whatsnew.txt"; # file to read

if (file_exists($filename)) {

# Get a handle to the asdfile
$file = file($filename);
echo "<p style='font-family:Arial; font-size:x-small'>";
foreach($file as $line) {
if (strpos($line,"\"") 0) {

$line = "<b>" . $line . "</b>";
}
$line .= "<br />";
echo $line;
}
echo "</p>";
}
You don't give any clue about how your code is failing to work so I won't
guess. But the most obvious error I can see is discarding lines that begin
with quotes. From manual:

"This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function."

http://es.php.net/strpos

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Oct 23 '08 #2

P: n/a
WebArchitect wrote:
I have a text file. My text file contains lines with double quotes
around it. I trying to code if a line has double quotes around it Then
make it bold

My code is below and it is not working. Please Help
<?

$filename = "whatsnew.txt"; # file to read

if (file_exists($filename)) {

# Get a handle to the asdfile
$file = file($filename);
echo "<p style='font-family:Arial; font-size:x-small'>";
foreach($file as $line) {
if (strpos($line,"\"") 0) {

$line = "<b>" . $line . "</b>";
}
$line .= "<br />";
echo $line;
}
echo "</p>";
}
?>
Your problem is how you're handling strpos(). Since you want to find
if quotes enclose a string, 0 will be the index at which the first
pair of quotes occur. Also, you should use htmlspecialchars() since
you're outputting data in an HTML document. A better way to test would be:

/* inside loop */
$line = htmlspecialchars($line);
if (strpos($line,'"') === 0 && strrpos($line,'"') == strlen($line) - 1)
echo "<b>$line</b><br />\n"
else
echo "$line<br />\n";

Although there's nothing wrong with how you handle output, I just used
a style, which, IMO, seems simpler; so feel free to take it or leave it.
--
Curtis
Oct 23 '08 #3

P: n/a
On Oct 23, 4:26*pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.comwrote:
*** WebArchitect escribió/wrote (Thu, 23 Oct 2008 09:53:22 -0700 (PDT)):


I have a text file. My text file contains lines with double quotes
around it. I trying to code if a line has double quotes around it Then
make it bold
My code is below and it is not working. Please Help
<?
$filename = "whatsnew.txt"; * *# file to read
if (file_exists($filename)) {
* * # Get a handle to the asdfile
* * $file = file($filename);
* * echo "<p style='font-family:Arial; font-size:x-small'>";
* * foreach($file as $line) {
* * if (strpos($line,"\"") 0) {
* * $line = "<b>" . $line . "</b>";
* * }
* * * * $line .= "<br />";
* * * *echo $line;
* * }
* * echo "</p>";
}

You don't give any clue about how your code is failing to work so I won't
guess. But the most obvious error I can see is discarding lines that begin
with quotes. From manual:

"This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing thereturn
value of this function."

http://es.php.net/strpos
When I first read that a few months ago about it may be FALSE, 0 or ""
I said, "Strange, but ok". Now wonder, why? And in what case would it
be FALSE, 0 or ""? It seems a vague explanation for something very
specific.

Bill H
Oct 24 '08 #4

P: n/a
Bill H wrote:
On Oct 23, 4:26 pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.comwrote:
>*** WebArchitect escribió/wrote (Thu, 23 Oct 2008 09:53:22 -0700 (PDT)):


>>I have a text file. My text file contains lines with double quotes
around it. I trying to code if a line has double quotes around it Then
make it bold
My code is below and it is not working. Please Help
<?
$filename = "whatsnew.txt"; # file to read
if (file_exists($filename)) {
# Get a handle to the asdfile
$file = file($filename);
echo "<p style='font-family:Arial; font-size:x-small'>";
foreach($file as $line) {
if (strpos($line,"\"") 0) {
$line = "<b>" . $line . "</b>";
}
$line .= "<br />";
echo $line;
}
echo "</p>";
}
You don't give any clue about how your code is failing to work so I won't
guess. But the most obvious error I can see is discarding lines that begin
with quotes. From manual:

"This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function."

http://es.php.net/strpos

When I first read that a few months ago about it may be FALSE, 0 or ""
I said, "Strange, but ok". Now wonder, why? And in what case would it
be FALSE, 0 or ""? It seems a vague explanation for something very
specific.

Bill H
It seems fairly clear. If the search failed, strpos() returns boolean
FALSE, as opposed to 0 or an empty string. The problem is that the
strpos() return value MUST differentiate between FALSE and 0. Take,
for example:

$haystack = '"foo."';
$pos = strpos($haystack, '"');
if (!$pos) {
echo 'No match. Maybe...';
} else {
echo 'Found match at index: ' . $pos;
}

$pos will hold 0, of type integer. So, while you may think you're
checking for the case in which no match occurred (using == or the
unary !), you will get unexpected results when a match was found at
index 0, as shown above. Thus the above conditional statement is
better written as:

if ($pos === false) {
echo 'Definitely no match.';
} else {
echo 'Found match at index: ' . $pos;
}

--
Curtis
Oct 24 '08 #5

P: n/a
On Oct 23, 8:57*pm, Curtis <dye...@gmail.comwrote:
Bill H wrote:
On Oct 23, 4:26 pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.comwrote:
*** WebArchitect escribió/wrote (Thu, 23 Oct 2008 09:53:22 -0700 (PDT)):
>I have a text file. My text file contains lines with double quotes
around it. I trying to code if a line has double quotes around it Then
make it bold
My code is below and it is not working. Please Help
<?
$filename = "whatsnew.txt"; * *# file to read
if (file_exists($filename)) {
* * # Get a handle to the asdfile
* * $file = file($filename);
* * echo "<p style='font-family:Arial; font-size:x-small'>";
* * foreach($file as $line) {
* * if (strpos($line,"\"") 0) {
* * $line = "<b>" . $line . "</b>";
* * }
* * * * $line .= "<br />";
* * * *echo $line;
* * }
* * echo "</p>";
}
You don't give any clue about how your code is failing to work so I won't
guess. But the most obvious error I can see is discarding lines that begin
with quotes. From manual:
"This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function."
>http://es.php.net/strpos
When I first read that a few months ago about it may be FALSE, 0 or ""
I said, "Strange, but ok". Now wonder, why? And in what case would it
be FALSE, 0 or ""? It seems a vague explanation for something very
specific.
Bill H

It seems fairly clear. If the search failed, strpos() returns boolean
FALSE, as opposed to 0 or an empty string. The problem is that the
strpos() return value MUST differentiate between FALSE and 0. Take,
for example:

$haystack = '"foo."';
$pos = strpos($haystack, '"');
if (!$pos) {
* *echo 'No match. Maybe...';} else {

* *echo 'Found match at index: ' . $pos;

}

$pos will hold 0, of type integer. So, while you may think you're
checking for the case in which no match occurred (using == or the
unary !), you will get unexpected results when a match was found at
index 0, as shown above. Thus the above conditional statement is
better written as:

if ($pos === false) {
* *echo 'Definitely no match.';} else {

* *echo 'Found match at index: ' . $pos;

}

--
Curtis- Hide quoted text -

- Show quoted text -
I get the FALSE and the 0, I just don't get having "" returned.

Bill H
Oct 24 '08 #6

P: n/a
Bill H wrote:
On Oct 23, 8:57 pm, Curtis <dye...@gmail.comwrote:
>Bill H wrote:
>>On Oct 23, 4:26 pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.comwrote:
*** WebArchitect escribió/wrote (Thu, 23 Oct 2008 09:53:22 -0700 (PDT)):
I have a text file. My text file contains lines with double quotes
around it. I trying to code if a line has double quotes around it Then
make it bold
My code is below and it is not working. Please Help
<?
$filename = "whatsnew.txt"; # file to read
if (file_exists($filename)) {
# Get a handle to the asdfile
$file = file($filename);
echo "<p style='font-family:Arial; font-size:x-small'>";
foreach($file as $line) {
if (strpos($line,"\"") 0) {
$line = "<b>" . $line . "</b>";
}
$line .= "<br />";
echo $line;
}
echo "</p>";
}
You don't give any clue about how your code is failing to work so I won't
guess. But the most obvious error I can see is discarding lines that begin
with quotes. From manual:
"This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function."
http://es.php.net/strpos
When I first read that a few months ago about it may be FALSE, 0 or ""
I said, "Strange, but ok". Now wonder, why? And in what case would it
be FALSE, 0 or ""? It seems a vague explanation for something very
specific.
Bill H
It seems fairly clear. If the search failed, strpos() returns boolean
FALSE, as opposed to 0 or an empty string. The problem is that the
strpos() return value MUST differentiate between FALSE and 0. Take,
for example:

$haystack = '"foo."';
$pos = strpos($haystack, '"');
if (!$pos) {
echo 'No match. Maybe...';} else {

echo 'Found match at index: ' . $pos;

}

$pos will hold 0, of type integer. So, while you may think you're
checking for the case in which no match occurred (using == or the
unary !), you will get unexpected results when a match was found at
index 0, as shown above. Thus the above conditional statement is
better written as:

if ($pos === false) {
echo 'Definitely no match.';} else {

echo 'Found match at index: ' . $pos;

}

--
Curtis- Hide quoted text -

- Show quoted text -

I get the FALSE and the 0, I just don't get having "" returned.

Bill H
It seems to me, they are simply stating it as an example. They are
referring to the fact that FALSE evaluates to 0 or an empty string,
not that strpos() can specifically return an empty string.*

I have never seen strpos() or strrpos() return an empty string,
although, this is just from my experiences using the functions...

* However, after more carefully reading the wording of the warning, it
does seem to suggest that an empty string may be returned, although I
have never found this to be the case. It is possible that the
documentation authors may have let such a suggestion slip through by
accident.

--
Curtis
Oct 24 '08 #7

P: n/a
On Oct 24, 5:48*am, Curtis <dye...@gmail.comwrote:
Bill H wrote:
On Oct 23, 8:57 pm, Curtis <dye...@gmail.comwrote:
Bill H wrote:
On Oct 23, 4:26 pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.comwrote:
*** WebArchitect escribió/wrote (Thu, 23 Oct 2008 09:53:22 -0700 (PDT)):
I have a text file. My text file contains lines with double quotes
around it. I trying to code if a line has double quotes around it Then
make it bold
My code is below and it is not working. Please Help
<?
$filename = "whatsnew.txt"; * *# file to read
if (file_exists($filename)) {
* * # Get a handle to the asdfile
* * $file = file($filename);
* * echo "<p style='font-family:Arial; font-size:x-small'>";
* * foreach($file as $line) {
* * if (strpos($line,"\"") 0) {
* * $line = "<b>" . $line . "</b>";
* * }
* * * * $line .= "<br />";
* * * *echo $line;
* * }
* * echo "</p>";
}
You don't give any clue about how your code is failing to work so I won't
guess. But the most obvious error I can see is discarding lines thatbegin
with quotes. From manual:
"This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function."
http://es.php.net/strpos
When I first read that a few months ago about it may be FALSE, 0 or ""
I said, "Strange, but ok". Now wonder, why? And in what case would it
be FALSE, 0 or ""? It seems a vague explanation for something very
specific.
Bill H
It seems fairly clear. If the search failed, strpos() returns boolean
FALSE, as opposed to 0 or an empty string. The problem is that the
strpos() return value MUST differentiate between FALSE and 0. Take,
for example:
$haystack = '"foo."';
$pos = strpos($haystack, '"');
if (!$pos) {
* *echo 'No match. Maybe...';} else {
* *echo 'Found match at index: ' . $pos;
}
$pos will hold 0, of type integer. So, while you may think you're
checking for the case in which no match occurred (using == or the
unary !), you will get unexpected results when a match was found at
index 0, as shown above. Thus the above conditional statement is
better written as:
if ($pos === false) {
* *echo 'Definitely no match.';} else {
* *echo 'Found match at index: ' . $pos;
}
--
Curtis- Hide quoted text -
- Show quoted text -
I get the FALSE and the 0, I just don't get having "" returned.
Bill H

It seems to me, they are simply stating it as an example. They are
referring to the fact that FALSE evaluates to 0 or an empty string,
not that strpos() can specifically return an empty string.*

I have never seen strpos() or strrpos() return an empty string,
although, this is just from my experiences using the functions...

* However, after more carefully reading the wording of the warning, it
does seem to suggest that an empty string may be returned, although I
have never found this to be the case. It is possible that the
documentation authors may have let such a suggestion slip through by
accident.

--
Curtis- Hide quoted text -

- Show quoted text -
I know it would just be a personal preference, but in the case of
strpos() (and maybe others, it would be nice if instead of FALSE if
not found, it returned -1 like Perl, Actionscript, Java and I believe
C... Would eliminate the need for the "Really equals" (what I call the
===).

I have always felt that FALSE should be -1, TRUE should be 1, since
everything in programming is zero-indexed.

Bill H
Oct 24 '08 #8

P: n/a
Bill H wrote:
On Oct 24, 5:48 am, Curtis <dye...@gmail.comwrote:
>Bill H wrote:
>>On Oct 23, 8:57 pm, Curtis <dye...@gmail.comwrote:
Bill H wrote:
On Oct 23, 4:26 pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.comwrote:
>*** WebArchitect escribió/wrote (Thu, 23 Oct 2008 09:53:22 -0700 (PDT)):
>>I have a text file. My text file contains lines with double quotes
>>around it. I trying to code if a line has double quotes around it Then
>>make it bold
>>My code is below and it is not working. Please Help
>><?
>>$filename = "whatsnew.txt"; # file to read
>>if (file_exists($filename)) {
>> # Get a handle to the asdfile
>> $file = file($filename);
>> echo "<p style='font-family:Arial; font-size:x-small'>";
>> foreach($file as $line) {
>> if (strpos($line,"\"") 0) {
>> $line = "<b>" . $line . "</b>";
>> }
>> $line .= "<br />";
>> echo $line;
>> }
>> echo "</p>";
>>}
>You don't give any clue about how your code is failing to work so I won't
>guess. But the most obvious error I can see is discarding lines that begin
>with quotes. From manual:
>"This function may return Boolean FALSE, but may also return a non-Boolean
>value which evaluates to FALSE, such as 0 or "". Please read the section on
>Booleans for more information. Use the === operator for testing the return
>value of this function."
>http://es.php.net/strpos
When I first read that a few months ago about it may be FALSE, 0 or ""
I said, "Strange, but ok". Now wonder, why? And in what case would it
be FALSE, 0 or ""? It seems a vague explanation for something very
specific.
Bill H
It seems fairly clear. If the search failed, strpos() returns boolean
FALSE, as opposed to 0 or an empty string. The problem is that the
strpos() return value MUST differentiate between FALSE and 0. Take,
for example:
$haystack = '"foo."';
$pos = strpos($haystack, '"');
if (!$pos) {
echo 'No match. Maybe...';} else {
echo 'Found match at index: ' . $pos;
}
$pos will hold 0, of type integer. So, while you may think you're
checking for the case in which no match occurred (using == or the
unary !), you will get unexpected results when a match was found at
index 0, as shown above. Thus the above conditional statement is
better written as:
if ($pos === false) {
echo 'Definitely no match.';} else {
echo 'Found match at index: ' . $pos;
}
--
Curtis- Hide quoted text -
- Show quoted text -
I get the FALSE and the 0, I just don't get having "" returned.
Bill H
It seems to me, they are simply stating it as an example. They are
referring to the fact that FALSE evaluates to 0 or an empty string,
not that strpos() can specifically return an empty string.*

I have never seen strpos() or strrpos() return an empty string,
although, this is just from my experiences using the functions...

* However, after more carefully reading the wording of the warning, it
does seem to suggest that an empty string may be returned, although I
have never found this to be the case. It is possible that the
documentation authors may have let such a suggestion slip through by
accident.

--
Curtis- Hide quoted text -

- Show quoted text -

I know it would just be a personal preference, but in the case of
strpos() (and maybe others, it would be nice if instead of FALSE if
not found, it returned -1 like Perl, Actionscript, Java and I believe
C... Would eliminate the need for the "Really equals" (what I call the
===).

I have always felt that FALSE should be -1, TRUE should be 1, since
everything in programming is zero-indexed.
Not necessarily, not all languages use zero-based indexeing, for
example. Perl also allows n-based indexing via the predefined var,
"$[". Also, Fortran and awk use 1-based indexing, IIRC.
Bill H
You could always write your custom wrapper for the strpos()
function(s). Something like:

<?php
function my_strpos($haystack, $needle, $offset = null)
{
return ($pos = strpos($haystack, $needle, $offset)) === false
? -1
: $pos;
}
?>

Although, personally, I wouldn't think it would be worth the trouble.
--
Curtis
Oct 25 '08 #9

P: n/a
On Oct 25, 5:36*am, Curtis <dye...@gmail.comwrote:
Bill H wrote:
On Oct 24, 5:48 am, Curtis <dye...@gmail.comwrote:
Bill H wrote:
On Oct 23, 8:57 pm, Curtis <dye...@gmail.comwrote:
Bill H wrote:
On Oct 23, 4:26 pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.comwrote:
*** WebArchitect escribió/wrote (Thu, 23 Oct 2008 09:53:22 -0700(PDT)):
>I have a text file. My text file contains lines with double quotes
>around it. I trying to code if a line has double quotes around itThen
>make it bold
>My code is below and it is not working. Please Help
><?
>$filename = "whatsnew.txt"; * *# file to read
>if (file_exists($filename)) {
>* * # Get a handle to the asdfile
>* * $file = file($filename);
>* * echo "<p style='font-family:Arial; font-size:x-small'>";
>* * foreach($file as $line) {
>* * if (strpos($line,"\"") 0) {
>* * $line = "<b>" . $line . "</b>";
>* * }
>* * * * $line .= "<br />";
>* * * *echo $line;
>* * }
>* * echo "</p>";
>}
You don't give any clue about how your code is failing to work so I won't
guess. But the most obvious error I can see is discarding lines that begin
with quotes. From manual:
"This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function."
>http://es.php.net/strpos
When I first read that a few months ago about it may be FALSE, 0 or""
I said, "Strange, but ok". Now wonder, why? And in what case would it
be FALSE, 0 or ""? It seems a vague explanation for something very
specific.
Bill H
It seems fairly clear. If the search failed, strpos() returns boolean
FALSE, as opposed to 0 or an empty string. The problem is that the
strpos() return value MUST differentiate between FALSE and 0. Take,
for example:
$haystack = '"foo."';
$pos = strpos($haystack, '"');
if (!$pos) {
* *echo 'No match. Maybe...';} else {
* *echo 'Found match at index: ' . $pos;
}
$pos will hold 0, of type integer. So, while you may think you're
checking for the case in which no match occurred (using == or the
unary !), you will get unexpected results when a match was found at
index 0, as shown above. Thus the above conditional statement is
better written as:
if ($pos === false) {
* *echo 'Definitely no match.';} else {
* *echo 'Found match at index: ' . $pos;
}
--
Curtis- Hide quoted text -
- Show quoted text -
I get the FALSE and the 0, I just don't get having "" returned.
Bill H
It seems to me, they are simply stating it as an example. They are
referring to the fact that FALSE evaluates to 0 or an empty string,
not that strpos() can specifically return an empty string.*
I have never seen strpos() or strrpos() return an empty string,
although, this is just from my experiences using the functions...
* However, after more carefully reading the wording of the warning, it
does seem to suggest that an empty string may be returned, although I
have never found this to be the case. It is possible that the
documentation authors may have let such a suggestion slip through by
accident.
--
Curtis- Hide quoted text -
- Show quoted text -
I know it would just be a personal preference, but in the case of
strpos() (and maybe others, it would be nice if instead of FALSE if
not found, it returned -1 like Perl, Actionscript, Java and I believe
C... Would eliminate the need for the "Really equals" (what I call the
===).
I have always felt that FALSE should be -1, TRUE should be 1, since
everything in programming is zero-indexed.

Not necessarily, not all languages use zero-based indexeing, for
example. Perl also allows n-based indexing via the predefined var,
"$[". Also, Fortran and awk use 1-based indexing, IIRC.
Bill H

You could always write your custom wrapper for the strpos()
function(s). Something like:

<?php
function my_strpos($haystack, $needle, $offset = null)
{
* *return ($pos = strpos($haystack, $needle, $offset)) === false
* * *? -1
* * *: $pos;}

?>

Although, personally, I wouldn't think it would be worth the trouble.
--
Curtis
Bill,

I guess this is just part of dealing with the loosely-typed nature of
php. the problem is that using:

"if($var)"
"empty($var)"
"isset($var)"

are shortcuts and the following usually evaluate to "FALSE" for the
above funcs and similar (nor a rule, more like a pattern):
0
0.0
""
"0"
false
null

so the point of the === operator is to avoid this kind of confusion
because in some contexts (string/array searches etc) the result 0 is
actually considered "TRUE". Curtis gave a pretty good example.
Oct 25 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.