3
I put the code in Paste bin: http://pastebin.com/vG6BSpuu
foreach ($linhas as $novalinha){
$novoTituloSlug = get_string_between($novalinha, 'id="', '">');
echo $novoTituloSlug;
$novalinhas = $novalinhas . "<link>http://localhost/site/" . $novoTituloSlug . "</link>" . "\r\n";
In the echo $novoTituloSlug;
got the result: valor1valor2valor3
.
But in the line below does not print the value, if I put echo I have the error:
Parse error: syntax error, Unexpected 'echo' (T_ECHO).
Follow the full code:
function get_string_between($string, $start, $end)
{
$string = " " . $string;
$ini = strpos($string, $start);
if ($ini == 0) return "";
$ini+= strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
// Lê conteúdo do txt
$documento = file_get_contents($arquivo);
$linhas = explode("<ul", $documento);
$novalinhas = "";
$novoPostId = 100;
foreach($linhas as $novalinha)
{
$novoTitulo = "TÍTULO I";
$novaData = date(DATE_RFC822);
$novaData2 = date("Y-m-d h:i:s");
$novoTituloSlug = get_string_between($novalinha, 'id="', '">');
echo $novoTituloSlug;
$novaCategoria = "constituicao-da-republica-federativa-do-brasil-de-1988";
$novoTituloCategoria = "CONSTITUIÇÃO DA REPÚBLICA FEDERATIVA DO BRASIL DE 1988";
$novalinhas = $novalinhas . "<item>" . "\r\n";
$novalinhas = $novalinhas . "<title>" . $novoTitulo . "</title>" . "\r\n";
$novalinhas = $novalinhas . "<link>http://localhost/votanalei/" . $novoTituloSlug . "</link>" . "\r\n";
$novalinhas = $novalinhas . "<pubDate>" . $novaData . "</pubDate>" . "\r\n";
$novalinhas = $novalinhas . "<dc:creator><![CDATA[ale]]></dc:creator>" . "\r\n";
$novalinhas = $novalinhas . "<guid isPermaLink='false'>http://localhost/votanalei/?p=" . $novoPostId . "</guid>" . "\r\n";
$novalinhas = $novalinhas . "<description></description>" . "\r\n";
$novalinhas = $novalinhas . "<content:encoded><![CDATA[<ul" . $novalinha . "]]></content:encoded>" . "\r\n";
$novalinhas = $novalinhas . "<excerpt:encoded><![CDATA[]]></excerpt:encoded>" . "\r\n";
$novalinhas = $novalinhas . "<wp:post_id>" . $novoPostId . "</wp:post_id>" . "\r\n";
$novalinhas = $novalinhas . "<wp:post_date>" . $novaData2 . "</wp:post_date>" . "\r\n";
$novalinhas = $novalinhas . "<wp:post_date_gmt>" . $novaData2 . "</wp:post_date_gmt>" . "\r\n";
$novalinhas = $novalinhas . "<wp:comment_status>open</wp:comment_status>" . "\r\n";
$novalinhas = $novalinhas . "<wp:ping_status>open</wp:ping_status>" . "\r\n";
$novalinhas = $novalinhas . "<wp:post_name>" . $novoTituloSlug . "</wp:post_name>" . "\r\n";
$novalinhas = $novalinhas . "<wp:status>publish</wp:status>" . "\r\n";
$novalinhas = $novalinhas . "<wp:post_parent>0</wp:post_parent>" . "\r\n";
$novalinhas = $novalinhas . "<wp:menu_order>0</wp:menu_order>" . "\r\n";
$novalinhas = $novalinhas . "<wp:post_type>post</wp:post_type>" . "\r\n";
$novalinhas = $novalinhas . "<wp:post_password></wp:post_password>" . "\r\n";
$novalinhas = $novalinhas . "<wp:is_sticky>0</wp:is_sticky>" . "\r\n";
$novalinhas = $novalinhas . "<category domain='category' nicename='" . $novaCategoria . "'><![CDATA[" . $novoTituloCategoria . "]]></category>" . "\r\n";
$novalinhas = $novalinhas . "<category domain='post_tag' nicename='" . $novaCategoria . "'><![CDATA[" . $novoTituloCategoria . "]]></category>" . "\r\n";
$novalinhas = $novalinhas . "<wp:postmeta>" . "\r\n";
$novalinhas = $novalinhas . "<wp:meta_key>_edit_last</wp:meta_key>" . "\r\n";
$novalinhas = $novalinhas . "<wp:meta_value><![CDATA[1]]></wp:meta_value>" . "\r\n";
$novalinhas = $novalinhas . "</wp:postmeta>" . "\r\n";
$novalinhas = $novalinhas . "</item>" . "\r\n";
$novoPostId = $novoPostId + 5;
}
$file = fopen("test.txt", "w");
$results = fwrite($file, $novalinhas);
fclose($file);
?>
If possible, post the complete code for a better understanding.
– MateusDemboski
Utilize
var_dump($novoTituloSlug);
to check what is in the variable.– Oeslei
returns - artigo1 string 'artigo1' (length=7) artigo2 string 'artigo2' (length=7) artigo3 string 'artigo3' (length=7)
– Alê Moraes
var_dump shows: string'artigo1' (length=7)
– Alê Moraes
In which line do you accuse the error? In what is the
echo
? Apart from theecho
it works normally?– GWER
The error is that it does not print on the link line, and if I place echo, this error appears: Parse error: syntax error, Unexpected 'echo' (T_ECHO)
– Alê Moraes
in this line: $novalinhas = $novalinhas . " <link>http://localhost/votanalei/" . $novoTituloSlug . " </link>" . " r n";
– Alê Moraes
Why do you use here "newline"
$novoTituloSlug = get_string_between($novalinha, 'id="', '">');
and here$novalinhas = $novalinhas . "<item>" . "\r\n";
"newlines"?– GWER
Because I wanted to concatenate, the lines. In foreach, it scans by array, and concatenates into the new lines.
– Alê Moraes
See if Voce has not forgotten to finish the previous line with point and comma ';'
– rafaelphp
I haven’t forgotten, have I, stranger? date values are printed smoothly, but I have a string that works with echo, but doesn’t print when I concatenate with a variable
– Alê Moraes
@Alêmoraes What error are you getting now? an alternative way to concatenate string is by using
.=
, see a example here.– stderr
The error is that it does not print on the link line, and if I place echo, this error appears: Parse error: syntax error, Unexpected 'echo' (T_ECHO). I put the code on this link: http://pastebin.com/vG6BSpuu
– Alê Moraes
@It was because you were putting the
echo ..
in a variable, see if that’s it: example.– stderr
This is not so, because, even taking the line 79 and 80 from your example, and taking the "s" from the end of the variable in line 84, does not give error, but also, does not print the variable in line 84, nor to stick, kkk
– Alê Moraes
In the function you take the content and put only "test"; Return, then it works, it prints the value.
– Alê Moraes