0

Hi i have the string like below,

$aa = "Ability: N/S,Session: Session #2: Tues June 14th - Fri June 24th (9-2:00PM),Time: 10:30am,cname: karthi";

$aa = "Ability: N/S,Session: Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time: #1 only: 1:30pm,cname: ravi";

$aa = "Ability: N/S,Session: Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time: #1 only: 1am,cname: mathi";

i need to write single regex for removing the particular string from ",cname:" upto last. i need output like,

$aa = "Ability: N/S,Session: Session #2: Tues June 14th - Fri June 24th (9-2:00PM),Time: 10:30am";

    $aa = "Ability: N/S,Session: Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time: #1 only: 1:30pm";

    $aa = "Ability: N/S,Session: Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time: #1 only: 1am";

how can i do this in regex?

6
  • So you want to remove everything after last comma? Commented Mar 2, 2012 at 13:17
  • Btw, you don't want to remove everything after ",cname:". You want to keep the last semicolon. Commented Mar 2, 2012 at 13:19
  • @m0skit0 yes. i need to remove after last comma. Commented Mar 2, 2012 at 13:20
  • yes i need semi colon.. i put semicolon for php format. thats it. actually the string is between the quotes. Commented Mar 2, 2012 at 13:20
  • If the string will always be in the same format, you don't need regex - you can simply $new = substr($str, 0, strpos($str, ',cname')); Commented Mar 2, 2012 at 13:21

4 Answers 4

1

You don't need regex for this. You can use strpos() to find the index of ',cname:' and then substr() up to that index.

<?php

$aa = "Ability: N/S,Session: Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time: #1 only: 1am,cname: mathi";
$pos = strpos($aa, ',cname:');
$bb = substr($aa, 0, $pos);
echo $bb, "\n";

but if you, for whatever reason, insist on using regex for this, you'll want to use preg_replace():

<?php

$aa = "Ability: N/S,Session: Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time: #1 only: 1am,cname: mathi";
$bb = preg_replace('#,cname:.*$#', '', $aa);
echo $bb, "\n";

and if you don't want to modify the string, you may want to use preg_match():

<?php

$aa = "Ability: N/S,Session: Session #1: Tues May 31st - Fri June 10th (1-5:30PM),Time: #1 only: 1am,cname: mathi";
if (preg_match('#^(.+),cname:.*$#', $aa, $match)) {
    echo $match[1], "\n";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Longer code = more potential bugs. Regex is shorter and cleaner.
For people knowing regular expressions they may seem cleaner. For those not too firm with regex, it just looks like mumbo jumbo. You also cannot argument against performance. Often times plain-string-function approaches outrun regex performance-wise. For this particular problem, I'd personally not have chosen regex.
Still: longer code = more bugs. Not knowing a technique is no excuse for not using it. If they look like mumbo jumbo, then learn so they don't.
1

Try

/,cname:.*$/

and replace with an empty string.

$result = preg_replace('/,cname:.*$/', '', $aa);

See it here on Regexr

1 Comment

Unnecessary replace operation.
0
/^(.*),cname:.*;$/

Group 1 ($1) generated by this regex will give you the result you want.

1 Comment

There is no semi-colon in the OP's string.
0

If you have more than one ,cname:... and just want to remove the last one, use this:

$aa= preg_replace('/,cname:.*?$/', '', $aa);

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.