Jump to content

trim text after open parentheseis


bad_gui

Recommended Posts

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?

Link to comment
Share on other sites

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"]);

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.  :o

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.