1

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
3
  • 1
    Are u sure that the first row's $row['name'] doesn't contain <br> at its end? Commented Nov 30, 2013 at 16:19
  • no there is no <br> or any other breaks in the mysql table. the second function takes vars from another col without any line breaks Commented Nov 30, 2013 at 16:25
  • ohh kevin thx for your great hint haha.. i deleted the firs row in my table and voila it works!! i looked for hours to fix this and i was not being able to see the wood for trees! thx for your input! Commented Nov 30, 2013 at 16:29

2 Answers 2

1

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

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

and what would be the regex for deleting brackets? I want to change "Course A (6 Weeks)" into "course-a-6-weeks" ?
just add () inside the [] so you end up with $string = preg_replace("/[-\s.()]+/", "-", $string); a good place to learn and test regex is rubular.com note: for your current example you would only want to add "(" not ")" or you'll end up with a trailing "-"
yes, regex is really useful but unfortunately i don't have the time to learn it right now. But thx for the link. I inserted () but the effect is "course-a-6-weeks-". Is it possible to delete the last "-" with regex or have i to go with with if & substr?
Since you would want to replace it with a different character (or in this case, no character) than the rest of the results, you would have to either do a 2nd replace or trim the last character off Easiest thing is a 2nd replace $string = preg_replace("/-\Z/", "", $string);
yes you are right! i took a 2nd substr /[-]?$/ and replace the last "-" if there is one ;-) rubular.com is nice! Thanks for your help and a good evening from vienna ;-)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.