0

hi have string,

$rt="Ability: B,Session: Session #2: Tues June 14th - Fri June 24th (9-2:00PM),Time: 9:30am,karthi";

$rt="Ability: B,Session: Session #2: Tues June 14th - Fri June 24th (9-2:00PM),Time: 9:30pm,karthi";

i used below regex for remove text from last comma(,).

$it_nme = preg_replace('/(?<=pm,)\S*/is', '', $rt);

it is worked for second string (because before comma have 'pm' text). for second one before comma we have string 'am'.

for both how can i write single regex?

1
  • 1
    As an aside: You really should learn at least the basics of regular expressions before using them in your code. Commented Mar 1, 2012 at 18:28

2 Answers 2

2
preg_replace('/(?<=[ap]m,)\S*/is', '', $rt)
Sign up to request clarification or add additional context in comments.

2 Comments

it replaces Fri June 24th (9-2:00PM) form here in string. but i need lowercase letter "pm".
@Ravichandran It should not replace the PM because the regex requires a , after the pm. To make the regex case-sensitive, remove the i modifier at the end.
0

You can use a regex OR like so:

$it_nme = preg_replace('/(?<=(pm|am),)\S*/is', '', $rt);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.