Help basename + url [PHP]

Asked

Viewed 33 times

0

I have that code:

if(count($vid_files)>0){
    $videoNames = array_keys($vid_files);
    $videoName = basename($videoNames[0]);
    $this->videourl = $videoName;

I would like to add a url at the beginning after the result of basename, the result of it is 100392video.mp4 I wanted the url to be in front, example.

$this->videourl = "url" + $videoName;

Only when I do that, the numbers come out.

100392

And no url comes out or the ending that is .mp4.

In answer to Isac;

    public function add($f3, $id, $name, $userid, $category, $vid_files){
        //add or edit to db
    if($id>=0){
        $this->load(array('id = ?',$id));
    }
    $this->userid = $userid;
    $this->name = $name;
    $this->category = $category;

    if(count($vid_files)>0){
        $videoNames = array_keys($vid_files);
        $videoName = basename($videoNames[0]);
        $this->videourl = $videoName;
    }

    $this->save();
}
  • 1

    var_dump($vid_files); before the if presents what?

  • 1

    My question was in the sense of realizing what exactly has its variable $vid_files that just by asking the question you can’t understand. Still in the answer (which should be an edition of the question) I do not think I put in the correct place. The idea is to put exactly before use, which would be before if(count($vid_files)>0){ and show us what comes out as a result of that dump

  • @Isac would be that?

  • 1

    Not so much, because it is not yet clear what $vid_files has. Place var_dump($vid_files); on the line just before if(count($vid_files)>0){. Then test again and put in question the result that the var_dump gave.

  • @Isac I can’t see what’s in vid_files.

  • @Isac has some idea?

  • 1

    basename will fetch the final part of the path, which will be the name of the file. However if you cannot find out what you are searching for in $vid_files[0] becomes impossible to help. Just trying to guess. By making a small analogy, it’s like trying to get a mechanic to fix your car without opening the hood, just for the sound of it. The only way he’s gonna get his car fixed is if he’s lucky enough to guess the problem from the sound.

  • @Isac da to join "URL" + $videoNames[0] ?

  • 1

    Gives, but concatenate in php is with . and not +, assuming we’re talking strings

  • @Our Isac, sorry! was just that, I forgot that to concatenate with php was with . kkkk, obg Isac :)

Show 5 more comments

1 answer

1


The problem is because you are "adding up" and not "concatenating".

change this:

$this->videourl = "url" + $videoName;

therefore:

$this->videourl = "url" . $videoName;

the old code sum numbers, in case url is valid 0 and video name 100392, therefore the result.

As it says in the documentation of PHP

String operators

There are two string operators. The first is the concatenation operator ('.'), that returns the concatenation of your right and left arguments. The second is the concatenation assignment operator ('.='), which adds the right-hand argument to the left-hand argument. See Assignment Operators for more information.

<?php
$a = "Olá ";
$b = $a . "mundo!"; // agora $b contém "Olá mundo!"

$a = "Olá ";
$a .= "mundo!"; // agora $a contém "Olá mundo!"
?>

For numbers yes, we use the +

echo 1+2; //mostra o numero 3

But calm

Because it is normal for these errors to happen, especially for those who develop in languages like java that string concatenation is done with the + also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.