Does not return the variable value

Asked

Viewed 158 times

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.

  • Utilize var_dump($novoTituloSlug); to check what is in the variable.

  • returns - artigo1 string 'artigo1' (length=7) artigo2 string 'artigo2' (length=7) artigo3 string 'artigo3' (length=7)

  • var_dump shows: string'artigo1' (length=7)

  • 1

    In which line do you accuse the error? In what is the echo? Apart from the echo it works normally?

  • 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)

  • in this line: $novalinhas = $novalinhas . " <link>http://localhost/votanalei/" . $novoTituloSlug . " </link>" . " r n";

  • 1

    Why do you use here "newline" $novoTituloSlug = get_string_between($novalinha, 'id="', '">'); and here $novalinhas = $novalinhas . "<item>" . "\r\n"; "newlines"?

  • 1

    Because I wanted to concatenate, the lines. In foreach, it scans by array, and concatenates into the new lines.

  • See if Voce has not forgotten to finish the previous line with point and comma ';'

  • 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 What error are you getting now? an alternative way to concatenate string is by using .=, see a example here.

  • 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

  • @It was because you were putting the echo .. in a variable, see if that’s it: example.

  • 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

  • In the function you take the content and put only "test"; Return, then it works, it prints the value.

Show 11 more comments

1 answer

0


Your code works.

function get_string_between(){
    return 'teste';
}

$linhas = array(1,2,3,4,6,7);

$novalinhas = '';

foreach ($linhas as $novalinha){
    $novoTituloSlug = get_string_between($novalinha, 'id="', '">');
    echo $novoTituloSlug;
    $novalinhas = $novalinhas .  "<link>http://localhost/site/" . $novoTituloSlug . "</link>" . "\r\n";
    echo $novalinhas;
}

Demo

What I believe is happening, is that in your local file, you forgot to type a period and comma ; or something like, follow an example code with the error you reported.

function get_string_between(){
    return 'teste';
}

$linhas = array(1,2,3,4,6,7);

$novalinhas = '';

foreach ($linhas as $novalinha){
    $novoTituloSlug = get_string_between($novalinha, 'id="', '">');
    echo $novoTituloSlug;
    $novalinhas = $novalinhas .  "<link>http://localhost/site/" . $novoTituloSlug . "</link>" . "\r\n"
    echo $novalinhas;
}

Demo

Working full code:

function get_string_between($string, $start, $end){
    return 'teste';
}

$linhas = array(1,2,3,4,5,6);
$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;
    echo $novalinhas;
}

Demo

  • Vlw Oedipus, from what I saw the problem is in this function, but, strange to work the echo $novoTituloSlug; and not working the $novalinhas = $novalinhas . "<link>http://localhost/votanalei/" . $novoTituloSlug . "</link>" . "\r\n";

  • there’s one left ; in that code you posted.

  • I put all the code here http://pastebin.com/vG6BSpuu

  • In this code I use 3 times the $novoTituloSlug, in the first testo with echo and it works, in the following two it does not work and it shows the error Notice: Undefined offset: 1

  • In the function you take the content and put only "test"; Return, then it works, it prints the value.

  • 2

    In that case, the problem is $linhas = explode("<ul", $documento);, the first item of the array is an empty string, when you do the explodes within the function quebra, you are standing on an empty string.

Show 1 more comment

Browser other questions tagged

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