1

I have this variable that contains a string. For example:

    $message    =   
    'Dear User1,

    Your record submitted has been disapproved by User.

    Remarks: 

    Document No.: 
    Record Title: Test1
    Record URL: http://myapp.dev/records?id=1

    Sincerely,

    Admin

    Dear User1,

    Your record submitted has been disapproved by User.

    Remarks: 

    Document No.: 
    Record Title: Test2
    Record URL: http://myapp.dev/records?id=2

    Sincerely,

    Admin';

What will I do in order to get this type of result?

    Dear User1,

    Your record submitted has been disapproved by User.

    Remarks: 

    Document No.: 
    Record Title: Test1
    Record URL: http://myapp.dev/records?id=1
    Record Title: Test2
    Record URL: http://myapp.dev/records?id=2

    Sincerely,

    Admin

I think I could manipulate the string by using preg_split();?

Please mention the things that I missed or you want to know. Any idea(s) would be really appreciated!

4
  • Show us what you have tried. Commented Nov 12, 2015 at 9:31
  • use html for this type of output. Commented Nov 12, 2015 at 9:33
  • Okay, I will edit my question Commented Nov 12, 2015 at 9:33
  • Sorry to ask @AwladLiton, I didn't get what you mean. Commented Nov 12, 2015 at 9:36

4 Answers 4

2

use nl2br() to get desired output.

echo nl2br($message);

Output

Dear User1,

Your record submitted has been disapproved by User.

Remarks: 

Document No.: 
Record Title: Test1
Record URL: http://myapp.dev/records?id=1

Sincerely,

Admin

Dear User1,

Your record submitted has been disapproved by User.

Remarks: 

Document No.: 
Record Title: Test2
Record URL: http://myapp.dev/records?id=2

Sincerely,

Admin

See demo

Sign up to request clarification or add additional context in comments.

4 Comments

There are more functions I'm now discovering! Much appreciated!
it's a bit different output on the OP's expected output
But it's still progress, I think I can use this function I suppose.
ya... you can wrap things using html tags.
1

better late than nothing. Try this function

function manipulate($text){
    $texts = preg_split("/(Remarks:|Sincerely,|Document No.:)/",$text,-1, PREG_SPLIT_DELIM_CAPTURE);
    $str = $texts[0];
    $str .= "Remarks: \n\nDocument No.:";
    for($x=0;$x<count($texts);$x++){
        if($texts[$x]=='Document No.:'){
            $str .= trim($texts[($x+1)],"\r\n");
        }   
    }
    $str.= "\n\nSincerely,\n\nAdmin";
    return $str;
}

if your using this one as html the use nl2br() like

nl2br(manipulate($message)); //where $message is your "Dear User1,...."

1 Comment

much appreciated man! this is awesome, I really need to improve though.
0

I think that you need to display in html page. Have tryied with nl2br() ?

I think that it should be better to use heredoc syntax, such as:

$message = <<< EOT_MSG
    'Dear User1,
     ...
     ....
EOT_MSG;

1 Comment

nl2br() is new to me, I will have a look on that function.
0

I think you can use something like this, an alternative to heredoc.

Here you can find the library. https://gist.github.com/fcamp/c4cb828cfb22bbe845cb

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.