Guys how to write a regex that delete everything after faqs?

$str1 = "/faqs/broker-type"; $str2 = "aaa <a href='/faqs/broker-type'>text</a>"; $str3 = 'aaa <a href="/faqs/broker-type">text</a>'; $patterns = [ '/faqs/' ]; $replacements = array(); echo preg_replace($patterns, $replacements, $str1); 

The result I want to get the following: That is, to delete everything after / faqs before single, double quotes and spaces.

  $str1 = "/faqs"; $str2 = "aaa <a href='/faqs'>text</a>"; $str3 = 'aaa <a href="/faqs">text</a>'; 

help me please

  • And links are always <a href='.. or there may be variants like <a target='_blank' href='...'> well, and double quotes instead of single quotes are found. This is all very complicated expression. If the links were the same, it would be possible to change something like /<a href='/faqs/\K.*?'/ change by one quote ... - Mike
  • @Mike Quotes can be double and single. I do not think that besides href may still be. But I know for sure that /faqs/ this is not found in the text /faqs/ That is, if there is /faqs/ then this is the line I need - user216109
  • Ah, I didn’t notice right away, without <a you are changing the same. on the one hand it is easier, on the other - it is necessary to understand where it will stop. you can cut after, for example, all non-whitespace characters to the next space or quotes /\/faqs\/\K.*?([\s'"]+)/ change to $1 that is, these are the quotes or whatever after the link - Mike
  • @Sergey, where did you end up with a single quote after /faqs/ in $ str2, and a double quote in $ str3? - Visman
  • @Mike I get it. trim until finished single, double or space - user216109

2 answers 2

 <?php $text="xyz /faqs/broker-type/abc/a.html not_replace aaa <a href='/faqs/broker-type/'>text</a> aaa <a href=\"/faqs/broker-type\">text</a> /faqs/abcdef/xyz.php"; print preg_replace('#/faqs\K.*?([\s\'"]|$)#','$1',$text); Результат: xyz /faqs/ not_replace aaa <a href='/faqs/'>text</a> aaa <a href="/faqs/">text</a> /faqs/ ?> 

Changes all similar places, the end of the link is considered whitespace or quotation marks. In the real text there may be some special cases that are not considered here, check.

  • It works fine, but if you can also remove the last slash, it will be gorgeous, if not, then I just delete the last character myself, thanks - user216109
  • @Sergey Well, then remove the check for it and everything (corrected in the answer), though then we can say, say /faqsnew/abc and the same will be cut off - Mike
  • All thanks, I'll figure it out myself. You're the bomb :) - user216109
  • one
    @Mike, regex101.com/r/5R6gdc/2 - Visman
  • @Visman Yes, I wanted to move the same \K first, but then I decided to make the oblique after faqs optional, all of a sudden it is not there ... although if it is not there, there is probably nothing to change ... - Mike
 <?php $array = [ '/faqs/broker-type', "aaa <a href='/faqs/broker-type'>text</a>", 'aaa <a href="/faqs/broker-type">text</a>', ]; foreach ($array as $key => $value) { $res = null; preg_match("/(.*\/faqs\/).*\K([\'\"].*)|(.*\/faqs\/)/", $value, $res); var_dump($res); } 
  • Nope, not quite that or not at all. - Visman
  • @Mike thanks for your efforts to crap me. Code tested and working. It works as I understood from the question. I get the first part and the second, after they can be glued together. If they are empty then another rule works. - Makarenko_I_V
  • @Makarenko_I_V I apologize, it really works like that, although I did not understand how to use these variables. but \K then why in the expression, what does she do? - Mike
  • This code does not work on the aaa /faqs/broker-type/ bbb , for example. - Visman
  • The @Mike \ K sequence can be used to reset the start of a match starting with PHP 5.2.4. Proof , In order for the results to have no text after '/ faqs /' before '. " - Makarenko_I_V