br3nn4n Posted May 4, 2009 Share Posted May 4, 2009 I know this is close to one of the most over-asked questions out there but here goes: I have a text file. It has lines like so: Background Color|backgroundcolor|#3356CC Each one is a setting. First is the friendly name, second part is the technical name, and third part is the value for the setting. Hope that makes sense. They're separated by the "|" character. I need to find the line in which a phrase exists. Say, I want to find the line with the site's title: Website Title|sitetitle|John's Hardware So the third element of that line. But how can I run through and do that? I've tried in_array, strpos...in various forms and if sequences. But I fail in this case...haha.. Thanks in advance for any help! Quote Link to comment https://forums.phpfreaks.com/topic/156755-find-a-word-in-a-string/ Share on other sites More sharing options...
lispwriter Posted May 4, 2009 Share Posted May 4, 2009 preg_match never fails me for finding stuff in strings. I'd loop through the file and test each line using that. so if you need to find 'sitetitle' then preg_match('/sitetitle/',$lineFromFile) will return something that evaluates to true when $lineFromFile has 'sitetitle' in it somewhere. that should find it just fine. if you open your text file into an array with file() then you can loop through the file very easily. you can also use explode() to break the line up like this: list($friendly,$name,$value) = explode("|",$lineFromFile); Quote Link to comment https://forums.phpfreaks.com/topic/156755-find-a-word-in-a-string/#findComment-825460 Share on other sites More sharing options...
Axeia Posted May 4, 2009 Share Posted May 4, 2009 although I agree with using explode, I wouldn't use preg_match. for straight text matches use strpos and !== instead of != as it might otherwise evualate things wrong when it's found on the very first position. (0 and false are equal to eachother using != ) if( strpos( $haystack, $needle ) !== false ) Quote Link to comment https://forums.phpfreaks.com/topic/156755-find-a-word-in-a-string/#findComment-825462 Share on other sites More sharing options...
Axeia Posted May 4, 2009 Share Posted May 4, 2009 I forgot to give a reason as to why didn't I? Quoting the preg_match page on php.net Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. if you're simply after the text after the last | in the string: echo strrchr( "Website Title|sitetitle|John's Hardware","|" ); Quote Link to comment https://forums.phpfreaks.com/topic/156755-find-a-word-in-a-string/#findComment-825469 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Since it's a text file, I would use strcmp() instead. Just load them into a variable using file() function, then loop through it. I think you can use in_array() too. Quote Link to comment https://forums.phpfreaks.com/topic/156755-find-a-word-in-a-string/#findComment-825618 Share on other sites More sharing options...
lispwriter Posted May 4, 2009 Share Posted May 4, 2009 After I thought about that again I'd only use preg_match there if I intended to snatch up the value after the last | with it as well // lazy style preg_match('/sitetitle\|(.*)/',$lineFromFile,$arMatches); // $arMatches[1] would contain "John's Hardware" Quote Link to comment https://forums.phpfreaks.com/topic/156755-find-a-word-in-a-string/#findComment-825758 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.