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.