bad_gui Posted April 7, 2008 Share Posted April 7, 2008 I'm modifying someone else's code. One problem is that when the app queries a public database some non-standard entries cause problems. For example the journal name: Science (New York, N.Y.) should be just Science Here is the code that strips HTML but how do I strip text after the open parenthesis? if (strstr($result_array[$i], "<Title>")) { $journal = utf8_decode(trim(strip_tags($result_array[$i]))); } Does this require regexp? Quote Link to comment Share on other sites More sharing options...
doni49 Posted April 7, 2008 Share Posted April 7, 2008 have a look at regular expressions. http://us.php.net/preg_matchall Quote Link to comment Share on other sites More sharing options...
bad_gui Posted April 10, 2008 Author Share Posted April 10, 2008 I couldn't figure out how to use regexp to delete text after and including certain characters. For example, I want to convert the following text Clinical cancer research : an official journal of the American Association for Cancer Research to simply this for uploading into my database Clinical cancer research The problem is that there are many journal titles with extra text after the relevant title and I don't want to write rules to substitute specific strings for each and every case. Here is a sed script that trims the text from the colon to the end. I can add a version of the sed command also for open parenthesis and dash etc. sed -i -e 's/:.*/ /' bogus.txt How would I apply this when the input text is coming from $_POST? $title = str_replace("\r\n", " ", $_POST["title"]); Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 10, 2008 Share Posted April 10, 2008 u would use preg_match , eg: preg_match("/\A[a-z0-9A-Z _]+/i",$title,$matches); $title = $matches[0]; hope this helps, if $matches[0] is not correct try using print_r to find the correct key, it should not change. Quote Link to comment Share on other sites More sharing options...
doni49 Posted April 10, 2008 Share Posted April 10, 2008 Maybe if you give us a bigger sample of your data, we might be better able to help. But right now, it looks like you want to grab everything before a colon and disregard from the colon on. In your first post above, it looked like you had "Science (New York, N.Y.)" and you wanted Science--in fact, that's what you said. Then you're talking about an sed script--WHAT is an sed script? Quote Link to comment Share on other sites More sharing options...
bad_gui Posted April 10, 2008 Author Share Posted April 10, 2008 my apologies. I am looking for a general tool that will remove the extra text after special characters such as : and ( in any string. Sed is an old-school unix/linux tool http://en.wikipedia.org/wiki/Sed Thanks uniflare! Your script works. I looked at the man pages for preg_match but didn't see how it would work for this purpose. Quote Link to comment Share on other sites More sharing options...
doni49 Posted April 10, 2008 Share Posted April 10, 2008 Thanks uniflare! Your script works. I looked at the man pages for preg_match but didn't see how it would work for this purpose. Uh--talk about contradictions. Don't see a use for preg_match, but the example provided (using preg_match) works. I dun't get that one. ??? Glad to see you got what you needed though. Quote Link to comment Share on other sites More sharing options...
bad_gui Posted April 15, 2008 Author Share Posted April 15, 2008 Yes. When I looked at the man page I saw that it would remove (for example) the last 7 characters after the @ symbol. What I couldn't see is how it would replace all of the text after special characters regardless of how long the string is (for example 7, 9 23 characters long). Thanks to all you php pros for awesome fixes. Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 15, 2008 Share Posted April 15, 2008 no problem, regular expressions is a very deadly tool when used correctly Quote Link to comment 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.