I have two lines:

<a href="/kp/pioneer-woman-glasses?cat_id=11327&create_ids=top-rtd-the-pioneer"></a> <a href="/kp/pioneer-woman-glasses?query=glasses&cat_id=11327&create_ids=top-rtd-the-pioneer"></a> 

Help trim a string through a regular expression to trim /kp/pioneer-woman-glasses to ?cat_id or ?cat_id

Tried ~a href=\"(.+?)(query|cat_id)~im , but it doesn't leave a href

  • one
    show what should be on exit. - Mikhail Vaysman
  • complete the question, in which function do you use the regular program? And yes, where is the expected output? - Gedweb
  • At the exit you need to get: <a href="?cat_id=11327&create_ids=top-rtd-the-pioneer"> </a> <a href="?query=glasses&cat_id=11327&create_ids=top-rtd-the-pioneer">> < / a> - Yuri
  • I wrote a solution, look in my answer - L. Vadim

1 answer 1

str_replace

 $str = "<a href='/kp/pioneer-woman-glasses?cat_id=11327&create_ids=top-rtd-the-pioneer'></a>"; $res = str_replace('/kp/pioneer-woman-glasses', '', $str); echo $res; 

regEx

 $str = "<a href='/kp/pioneer-woman-glasses?cat_id=11327&create_ids=top-rtd-the-pioneer'></a>"; $res = preg_replace("/\/kp(.+?)(ses)/", "", $str ); echo $res; 
  • You can write the answer if in URl there is not only pioneer-woman-glasses, but for example cars - Yuri
  • preg_replace ("/ \ / kp (. +?) (ses | cars) /", "", $ str); - L. Vadim
  • helped you ???? - L. Vadim
  • yes, thanks helped - Yuri