How can I prepend a string, say 'a' stored in the variable $x to each line of a multi-line string variable using PHP?
How can I prepend a string, say ‘a’ stored in the variable $x to
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because of your each line requirement, I would first split the string to an array using
explode, then loop through the array and add text to the beginning of each line, and then turn the array back to a string usingimplode. As long as the number of lines is not very big, this can be a suitable solution.Code sample:
Example at: http://codepad.org/0WpJ41LE