It is currently Sat May 25, 2013 11:58 am

All times are UTC


You can access below Applications



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: how to search a string in a stirng using php
PostPosted: Wed Jun 02, 2010 12:03 pm 
Offline

Joined: Tue Mar 02, 2010 11:25 am
Posts: 190
Location: Kovilpatti
Hi

how to search a particular string in a string

for example i want to search a string "this " in the sentence "this is the new world"

using strpos() we search the string and identify the position of the string

sample code:
Code:
<?php
$str
="hai this is new world";
$pos=strpos($str,"this");
if(
$pos==false)
{
 echo 
"search string not found";
 
}
else
{
 echo 
"the search string found in position".$pos;
}
?>
Share |

_________________
Thanks,
A.ThangaSornam,

QualityPointTechnologies,
Ottapidaram.


Top
 Profile  
 
 Post subject: Re: how to search a string in a stirng using php
PostPosted: Wed Jun 02, 2010 12:55 pm 
Offline
User avatar

Joined: Tue Mar 02, 2010 10:49 am
Posts: 171
Location: India/Tamilnadu/Tirunelveli
You can also use strstr() php function for find whether the given input string is available or not.

Code:
<?php
$str="hai this is new world";
$pos=strstr($str,"this");
if($pos==false)
{
echo "search string not found";

}
else
{
echo "the search string found in position".$pos;
}
?>


It returns the portion of string, or FALSE if needle is not found.

This function is case-sensitive. For case-insensitive searches, use stristr().

The equivalent function for search whether a particular character or substring exists within a string in Javascript is, indexOf().

The indexOf() function allows you to search whether a particular character or substring exists within a string, if so , where the first character of the character/substring is located.

If no match is found, "-1" is returned instead. See below code.

Code:
var str="hai this is new world"
if (str.indexOf("hai")!=-1)
alert("hai is in there!")



Anbarasan k
Share |

_________________
Anbarasan k


Top
 Profile  
 
 Post subject: Re: how to search a string in a stirng using php
PostPosted: Thu Jun 03, 2010 6:01 am 
Offline

Joined: Thu Jun 03, 2010 5:53 am
Posts: 10
strpos is fastest function to use but have in mind return value of 0 is perfectly valid position!!!
So check should be type aware. Pretty big warning mark there on http://php.net/strpos

Code:
if ($pos === false) { // mind the ===

}
Share |


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron