PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to erase a string and what comes after in PHP?

 November 20, 2022     php, preg-replace     No comments   

Issue

i'm trying to get rid of a few character within a string:

"V.I.E Responsable Projets Marque Propre (H/F) – Roumanie (Bucarest) – 12 mois renouvelables."

I'd like to erase everything after (H/F) or (h/f) or H/F or F/H or f/h etc ...

I tried this but not working:

$offer = preg_replace(array('/ F\/\H.*/','/ (F\/\H).*/','/ H\/\F.*/','/ (H\/\F).*/'), '', $offer);

Any idea ? Thanks a lot from France !


Solution

Find the pattern \s*\(?(?:h/f|f/h)\)?.*$ and then replace with empty string:

$input = "V.I.E Responsable Projets Marque Propre (H/F) – Roumanie (Bucarest) – 12 mois renouvelables.";
$output = preg_replace("/\s*\(?(?:h\/f|f\/h)\)?.*$/i", "", $input);
echo $input . "\n" . $output;

This prints:

V.I.E Responsable Projets Marque Propre (H/F) – Roumanie (Bucarest) – 12 mois renouvelables.
V.I.E Responsable Projets Marque Propre


Answered By - Tim Biegeleisen
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing