Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
Hello,

while nl2br does in fact generate <br /> tags as expected, the original newline control character in a textarea field does not get removed, which is problematic since I'm trying to get that textarea value into a json string.

Any advice?

Hi,

The cms:nl2br tag is just a thin wrapper around PHP nl2br() function which, as PHP docs state -
returns string with '<br />' or '<br>' inserted before all newlines (\r\n, \n\r, \n and \r).

So, yes, that is the expected behavior of the tag.

For your particular use-case, you may use the following custom tag.
Please place this in your addosn/kfunctions.php file -
Code: Select all
// TAG: cms:nl2br_ex
// Completely removes newlines from nl2br
$FUNCS->register_tag( 'nl2br_ex', nl2br_ex_handler );
function nl2br_ex_handler( $params, $node ){
    global $FUNCS;

    // call the children
    foreach( $node->children as $child ){
        $html .= $child->get_HTML();
    }

    $html = str_replace(array("\r", "\n"), '', nl2br($html) );
    return $html;
}

Now instead of cms:nl2br, use cms:nl2br_ex instead e.g.
Code: Select all
<cms:nl2br_ex><cms:show str /></cms:nl2br_ex>

Hope it helps.

Hi,

thanks a ton for the quick reply, I will try this right away. (I assumed nl2br should remove the newlines because the doc page states that it replaces newlines with br tags).

However, in my attempt to go solve this myself I have encountered another extremely weird bug.

So first I do this:

Code: Select all
<cms:set status = "<cms:input class='statusdummy' name="<cms:show 'statusdummy' /><cms:show k_page_id />" type='dropdown' opt_values='offen|in Evidenz|genehmigt TL|genehmigt|abgelehnt|storniert' opt_selected="<cms:show status />" />" />


This results in this output later in json:

Code: Select all
"<select
        name="statusdummy10944"
        id="statusdummy10944"
                        class="statusdummy"    >
                    <option value="offen" >offen</option>
                    <option value="in Evidenz" >in Evidenz</option>
                    <option value="genehmigt TL" selected="selected">genehmigt TL</option>
                    <option value="genehmigt" >genehmigt</option>
                    <option value="abgelehnt" >abgelehnt</option>
                    <option value="storniert" >storniert</option>
            </select>"



So now I tried to use php to get rid of the newlines:

Code: Select all
<cms:set status = "<cms:php> 
            echo(trim(preg_replace('/\s+/', ' ', \"><cms:addslashes><cms:addslashes><cms:addslashes><cms:show status /></cms:addslashes></cms:addslashes></cms:addslashes>\")));
            </cms:php>"
/>


While this produces valid json, it for some reason adds an extra ">" at the start of string. I have no idea why:

Code: Select all
"><select name=\"statusdummy10944\" id=\"statusdummy10944\" class=\"statusdummy\" > <option value=\"offen\" >offen</option> <option value=\"in Evidenz\" >in Evidenz</option> <option value=\"genehmigt TL\" selected=\"selected\">genehmigt TL</option> <option value=\"genehmigt\" >genehmigt</option> <option value=\"abgelehnt\" >abgelehnt</option> <option value=\"storniert\" >storniert</option> </select>"


Maybe I'm just missing something extremely obvious. Still, I will try your solution right now :)

Quick update, I had to modify your posted solution a little but it's working perfectly fine now :)

Also I'm an idiot and found the reason for the extra >


:echo(trim(preg_replace('/\s+/', ' ', \"><cms:addslashes>

So dumb...

Thanks again for your help :)

Glad it helped :)

I had to modify your posted solution a little ..

Mind sharing the modification? It could help others. Thanks.

Sure.

Since cms input generates linebreaks inside the eg. select tags, replacing newlines with <br /> wouldnt work because the br tags would break the other tags, so I just made two tags - additionally one that just removed the newlines alltogehter:

Code: Select all
// TAG: cms:nl2br_ex
// Completely removes newlines from nl2br
$FUNCS->register_tag( 'nl2br_ex', nl2br_ex_handler );
function nl2br_ex_handler( $params, $node ){
    global $FUNCS;

    // call the children
    foreach( $node->children as $child ){
        $html .= $child->get_HTML();
    }

    $html = str_replace(array("\r", "\n"), '', nl2br($html));
    return $html;
}

// TAG: cms:nlrem
// Completely removes newlines and whitespace
$FUNCS->register_tag( 'nlrem', nlrem_handler );
function nlrem_handler( $params, $node ){
    global $FUNCS;

    // call the children
    foreach( $node->children as $child ){
        $html .= $child->get_HTML();
    }

    $html = str_replace(array("\r", "\n"), '', preg_replace('/\s+/', ' ', $html));
    return $html;
}
6 posts Page 1 of 1

Who is online

In total there are 3 users online :: 1 registered, 0 hidden and 2 guests
(based on users active over the past 5 minutes)

Users browsing this forum: Google [Bot] and 2 guests