Not only did I understand a little less than half of it in regulars, I also complicated the task n-times myself! In the text, the paths to the image files to the new paths are replaced, in addition, these updated paths are again used to copy the file data. I replaced the ways as follows:

$text = preg_replace("#images/(.*)'#" , "/anotherDir/images/$1'",$text); 

It may be clumsy, but it works. In the following lines of code you need to copy the files involved in the renaming to the above directory. Those. you need to somehow save all these $ 1 for further use. I generally get lost. What function to apply. Whether preg_match_all whether ... well, tell me who can! At least the course of the decision.

  • There are such functions as dirname and basename , as well as pathinfo - zb '
  • @eicto, these are great features, but they won't suit me. Files pieces 200, and you need to copy only those that were mentioned in $ text - Deus
  • The answer @klopp does what is necessary, but it is better to change the regular expression to something like this: /images/(.+\.(?:jpg|png|bmp|gif))$ just for security, otherwise you might see something like: / images /../index.php in the text in which you want to convert the path. - ReinRaus
  • @ReinRaus, you are right. Only I did not bother much about protection, this is a kagbe page for administration kagbe, then - thanks for the advice! - Deus

1 answer 1

Somehow you can:

 function replace_and_move( $m ) { // ну или не переименовывать, а где-то сохранить значения: rename( $m[1].$m[2], '/anotherDir/images/'.$m[2] ); return '/anotherDir/images/'.$m[2]; } $text = preg_replace_callback("#(images/)(.*)#" , 'replace_and_move', $text); 
  • @ klopp, thanks for the reply. But I just guessed it - added another regular expression preg_match_all ("# images / ([a-zA-Z0-9 \.] *) '#", $ Text, $ matches). Those. in $ matches, these are now the file names. So it is possible? )) It just works, but there are already two regulars! - Deus
  • @klopp, you not only advised, but also gave a complete solution. Thank. Besides, now I know in which direction to move on! - Deus