简体   繁体   中英

php line break issue - function return in echo of while loop

My Problem ist that the two first function echos in the while loop break the line in the code, after this it goes fine.

Function:

function firstFunction($string) {
$search = array(' - ',' ','.');
$replace = array('-','-','-');
$string = strtolower(str_replace($search,$replace,$string));
return $string;
}

My while Loop:

... 
while($row = mysql_fetch_array($result)){ 
echo '
<!-- '; echo firstFunction($row['name']).' -->
blabla '; echo secondFunction().' blabla
';
};
...

Effect in Source Code:

<!-- course-a
-->blabla secondFResult
blabla
<!-- course-b -->
blabla secondFresult blabla
<!-- course-c -->
blabla secondFresult blabla

I want it to go this way:

<!-- course-a -->
blabla secondFresult blabla
<!-- course-b -->
blabla secondFresult blabla
<!-- course-c -->
blabla secondFresult blabla

I saw you already got the problem fixed but i don't have the reputation to comment yet and wanted to throw this bit of info your way

with your current firstFunction if the data is a - b. c a - b. c

you'll end up with something like a--b--c

i would suggest you change it to

function firstFunction($string) {
    $string = preg_replace("/[-\s.]+/", "-", $string);
    return $string;
}

It is probably because the name key's value contains a line break appended to it in the database. Try fixing it either by re-entering the value or by filtering it in the firstfunction() function of yours.

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