0

I have the same question but in the Spanish forum, but because I don't get any suggestions I ask it here. Since I can't find another solution

I try to change part of some URLs of various post entries, I manage to do it in the entries that do not present many complications in the url of the images, however those that have varied routes with interspersed spaces or double points throw me an error.

As I consult them from a database, I do the test with the one that has the id that generates the error in the subject of this post.

test code that I use to change parts of the image URLs.

$data= User::find(2);

  $html = $data->name;
$html = "<body>$html</body>";


$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_COMPACT | LIBXML_HTML_NOIMPLIED | LIBXML_NONET | LIBXML_HTML_NODEFDTD);
if (is_null($dom)) {
} else {
  $a_nodelist = $dom->getElementsByTagName('p');

  foreach ($a_nodelist as $enlace) {
    $img = $enlace->getElementsByTagName('img')->item(0);
    if ($img) {
      $urlImagen = $img->getAttribute('src');
      $imagenArreglo = explode("/", $urlImagen);
      $nombreImagen = end($imagenArreglo);




      $rutasinImagen = rtrim($urlImagen, $nombreImagen);

      dd($rutasinImagen);

      $nuevaruta = 'http://www.rumberos.net/wp-content/uploads/2022/imagen/';
      $file = fopen("archivo.txt", "a");
      fwrite($file, $data->id . PHP_EOL);
      fclose($file);

      DB::table("users")
        ->where("id", "=", $data->id)
        ->update(["name" => DB::raw("REPLACE(name,  '$rutasinImagen', '$nuevaruta')")]);
    }

The posts have HTML code so to get the url I use dom. It works for some publications, but I have a feeling that it will fail for those with messy urls. For example, the entry that fails with the attached error has the following content.

http://midominio.com/images/2014/HTV- Premios Heat - Juanes..jpg

I do the DD when I separate the string to get the name of the image, and it works fine until there, but when I try to delete that part of the URL it throws me the error. I think it must be because it has the double dots and spaces.

1 Answers1

0

By definition, a url can not have spaces. Is a URL allowed to contain a space?

Your url will never work. You have to ensure that the files you upload have proper names. you can use php trim() to remove the whitespaces from the file name before saving it in database, but if the file in question already has space in its name there is not really much you can do there. One option is doing this to manually convert the url before posting it. This will convert the empty spaces in your url to how the browser will see it as.

$image_url = str_replace(' ', '%20', $name_of_image);

This will convert your url to http://midominio.com/images/2014/HTV-%20Premios%20Heat%20-%20Juanes..jpg, which is how your browser will read it as when you try to hit it. But I must warn you that this is not a proper solution, more like a band aid at best. This will work, but there is a reason why whitespaces are not allowed in urls. they are a huge security threat. If you can, I suggest finding a way to ensure the file names do not have whitespaces or double dots in the first place.

Edit: Ok, based on your update, you need additional fixes. To fix the double dots, simply use string replace before trying to use the url. you need to do the string fixing before you try to use it. You must first replace the four dots and then the two dots. Unfortunately without seeing your db, I have no idea if you might encounter any other number of dots, but the principle remains the same.

 str_replace('.', '....', $your_string);
 str_replace('.', '..', $your_string);
Mash tan
  • 159
  • 11
  • Hello, thank you for your answer, yes indeed, many of the routes are poorly saved, my job is to fix all that bad practice, but I come up with the problem that I cannot remove the double points or the four points that some routes have, some advice to deal with such varied data? What I need is to eliminate those extra points that have many urls by name – Isaac Lovera Jun 26 '22 at 00:31
  • It is the best way, grateful for your support, of course, there are more than 40,000 records in the database, so I am writing down my points of failure to know in which record number the algorithm fails to adjust it. I can't think of another way, if you have any suggestions I will be grateful – Isaac Lovera Jun 27 '22 at 18:00
  • @IsaacLovera glad to hear it. Unfortunately, without running a query to actually change the file names in database, string replace might be the only way to resolve this. And considering there are over 40k data in the database, I would be very wary of running any query that affects all of them. Something can very easily go wrong. While it is something to ponder about in the future, I would not recommend you trying it here to fix your database records. Your best option here is indeed to modify how the database records are interpreted in your code. – Mash tan Jun 28 '22 at 03:38