简体   繁体   中英

PHP - SimpleXML Using Variables Created From Objects Made in ForEach Loop

I am trying to develop a navigation breadcrumb system for my new website. My page uses SimpleXML to retrieve it's file name and page information from an xml database.

I am recieving php error for the php错误 function in the last code block: Parse error: syntax error, unexpected T_STRING in /home/content/85/8762385/html/joelsdesign/movies/we-are-hiring.php on line 21

In the head of the document, there is

This is the contents of my xml file: http://joelsdesign.info/movies/scores.xml

My navigation menu is 'required' and the require statement is stored in my variable called 变量中

foreach ($dom->page as $page) {
    $title = $page->title;
    $file = $page->file;
    $nav = file_get_contents('require/nav.php');
    $nav = preg_replace("<li><a href=\"".$file."\">".$title."</a></li>", "<li class="current"><a href=\"".$file."\">".$title."</a></li>", $nav);
}

The quotes around the word current were not escaped:

$nav = preg_replace("<li><a href=\"".$file."\">".$title."</a></li>", "<li class=\"current\"><a href=\"".$file."\">".$title."</a></li>", $nav);

Also you shouldn't be using preg_replace without using a regex. You should be using str_replace.

If you actually needed preg_replace you would have written a regex - Regular Expression - that needs to start with a delimiter most often the forward slash / . Hence the second error is created by the fact that < is not a known delimiter for a regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM