Parse url for filename

New to PHP and having some trouble. I am attempting to parse a url and assign the filename part to a variable to be stored in xml file. I have the xml file structure setup and I know it works. I am able to parse the filename but only able to echo it, I have been unable to assign it to a variable to insert into xml. Here is code:

    [php]$url="http://myserver/mydirectory/filename.txt"
              $path_parts = pathinfo($url);
              echo $path_parts['filename'], "\n";
    [/php]

When I attempt to assign it to variable like:
$myvar=$path_parts[‘filename’];
I get Illegal string offset ‘filename’ error.

Can I get some guidance as to

Your missing a semicolon on the first line.

Well like I said new to PHP. When I attempt to assign it to my xml

$shownameText= $xml->createTextNode($path_parts[‘filename’]->nodeValue);

It gives me errors and will not create a node.
I get this error:
Trying to get property of non-object

It does if I just want to echo to screen but I want to insert into xml:
$shownameText= $xml->createTextNode($path_parts[‘filename’]->nodeValue);
That’s where I get the error is on this line.

Gives me:
syntax error, unexpected ‘{’, expecting ‘)’

Post the whole code your working with.

<?php $ch = curl_init("http://xxxxxx.xxxx/hhhhhh/kkkkk.htm"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); $html = curl_exec($ch); curl_close($ch); #CREATE DOM PARSER $xml = new DOMDocument("1.0"); $root = $xml->createElement("programme"); $book = $xml->createElement("tvprogram"); $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); $dom->formatOutput = true; $dom->preserveWhiteSpace = true; $images= $dom->getElementsByTagName('img'); foreach($images as $img){ $icon= $img ->getAttribute('src'); if( preg_match('/\.(jpg|jpeg|gif)(?:[\?\#].*)?$/i', $icon) ) { //only matching types } [b]$path_parts = pathinfo($icon); $programname=$path_parts['filename'];[/b] $xml->appendChild($root); #$title = $xml->createElement("Channel"); //CHANNEL NAME $id = $xml->createElement("imageurl"); //CHANNEL IMAGE $showname= $xml->createElement("programname"); //PROGRAM NAME #$showtime= $xml->createElement("programtime"); //PROGRAM TIME #$descriptime= $xml->createElement("description"); //PROGRAM DESCRIPTION #$titleText = $xml->createTextNode($channel); $idText= $xml->createtextNode($icon); [b]$shownameText= $xml->createTextNode($programname->nodeValue);[/b] #$showtimeText= $xml->createTextNode($programtime->nodeValue); #$showdescripText= $xml->createTextNode($descrip->nodeValue); #$title->appendChild($titleText); $id->appendChild($idText); $showname->appendChild($shownameText); #$showtime->appendChild($showtimeText); #$descriptime->appendchild($showdescripText); #$book->appendChild($title); $book->appendChild($id); $book->appendchild($showname); #$book->appendchild($showtime); #$book->appendchild($descriptime); $root->appendChild($book); $xml->formatOutput = true; $xml->save("horror.xml") or die("Error"); } ?>

Should be fairly simple, just wanting to extract the filename from the http url that I am pulling the img from. I have other elements # , I am not interested in those at this time for this particular project. Thanks for looking

I got it, really don’t know how but removing the ->nodeValue

$shownameText= $xml->createTextNode($programname);

solved the problem.

Thanks for looking

Sponsor our Newsletter | Privacy Policy | Terms of Service