Jump to content

Recommended Posts

hello,

 

I have a string that indicates name and surname or company, if its company string will have S.A, SA, LTD, L.T.D etc inside the string. if its name will have LISA SMITH inside the string. Mind the liSA. i think that my pattern has to take into consideration the white spaces. I am trying for hours with preg_match but without the desired results :( I hope someone knows how to resolve it.

 

the pseudo code is below:

 

foreach ($str as $str1)

 

if $str1 has inside S.A or SA or LTD or L.T.D between white spaces

then $form = "company"

else $form = "name"

 

 

thank you in advance

Link to comment
https://forums.phpfreaks.com/topic/89701-solved-search-in-string/
Share on other sites

<php
$search_co=array("sa","SA","LTD","L.T.D");
$flag=false;
$string="test sa";

$arr = explode(" ",$string);
foreach($search_co as $co){
	if(array_search($co,$arr,true)){
		$flag=true;
		break;
	}		
}
if($flag){
	echo "This is company";
}else{
	echo "This is name";
}

?>

 

What exactly is \LTD and \b ???

These should cause a PHP warning.

 

I know that preg_match("/LTD/i", $name) will work. How about you try it

 

In fact if you'd bothered to try my regular expression i posted above you would find that it does EXACTLY the job you require.

try

<?php
$array =array('xltd kfjrkls sltd','x LTD kfjrkls sltd','ltd kfjrkls sltd','xltd kfjrkls s ltd v','xltd kfjrkls s ltd ');
foreach ($array as $a){
$b = preg_match('/(^|\s)((ltd)|(l\.t\.d)|(sa)|(s\.a))( |$)/i',$a);
echo $a, ' --> ', $b, "<br />\n";}
?>

@aschk I found this piece of code in php.net it works without errors... ut for others not for me :P

 

@pdkv2 Thank you SooooOoOoOoooooOO much it works perfectly, is very straight forward to understand it, very easy to add more words and very efficient with the break command... amazing

 

thank you again...

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.