snk Posted February 6, 2008 Share Posted February 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89701-solved-search-in-string/ Share on other sites More sharing options...
aschk Posted February 6, 2008 Share Posted February 6, 2008 Using preg_match you should have a regular expression something like : $regex = '/(S[.]?A)|(L[.]?T[.]?D)/'; I think... Quote Link to comment https://forums.phpfreaks.com/topic/89701-solved-search-in-string/#findComment-459643 Share on other sites More sharing options...
pdkv2 Posted February 6, 2008 Share Posted February 6, 2008 <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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89701-solved-search-in-string/#findComment-459655 Share on other sites More sharing options...
snk Posted February 6, 2008 Author Share Posted February 6, 2008 I use if (preg_match("/\LTD\b/i", $name)) { $form = "company"; } else { $form = "name"; } but when $name is for example ACTION LTD $form is still "name" instead of "company" any help? Quote Link to comment https://forums.phpfreaks.com/topic/89701-solved-search-in-string/#findComment-459658 Share on other sites More sharing options...
aschk Posted February 6, 2008 Share Posted February 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89701-solved-search-in-string/#findComment-459661 Share on other sites More sharing options...
sasa Posted February 6, 2008 Share Posted February 6, 2008 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";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/89701-solved-search-in-string/#findComment-459664 Share on other sites More sharing options...
snk Posted February 6, 2008 Author Share Posted February 6, 2008 @aschk I found this piece of code in php.net it works without errors... ut for others not for me @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... Quote Link to comment https://forums.phpfreaks.com/topic/89701-solved-search-in-string/#findComment-459667 Share on other sites More sharing options...
snk Posted February 6, 2008 Author Share Posted February 6, 2008 @ sasa thank you for your help, I have resolved it with pdkv2's code. I will evaluate your code in an other problem. Thank again for your effort and time Thank you all... Quote Link to comment https://forums.phpfreaks.com/topic/89701-solved-search-in-string/#findComment-459670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.