CodeIgniter Forums
[Solved] Using preg_replace_callback with a href - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Solved] Using preg_replace_callback with a href (/showthread.php?tid=66521)



[Solved] Using preg_replace_callback with a href - wolfgang1983 - 10-31-2016

In my preg_replace_callback I can allow some elements to be showing in preview using the preg_replace_callback

How ever when the user has typed in a link lets say <a href="http://www.example.com">Example</a>

The preview will only put out <a href="http://www.example.com">Example

Is there any way I can integrate this


PHP Code:
preg_replace("~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~","<a href=\"\\0\">\\0</a>"$string


Into the code below


PHP Code:
public function preview() {
    $data = array('success' => false'question' => '');

    if ($_POST) {

        $string $this->input->post('question');

           $match = array(
            '<' => '&lt;',
            '>' => '&gt;',
        );

        $new_data preg_replace_callback("#</?(pre|code|h1|h2|h3|h4|h5|h6|b|strong|i|u|hr)>|[<>]#", function ($match) {
            return $match[0] == '<' '&lt;' : ($match[0] == '>' '&gt;' $match[0]);
        }, $string);

        $data['question'] = parse_smileys($new_database_url('assets/img/smiley'));
        $data['success'] = true;
    }

    $this->output
           
->set_content_type('application/json')
        ->set_output(json_encode($data));




RE: Using preg_replace_callback with a href - InsiteFX - 10-31-2016

If it's dropping the end of the link do a check and if it's a link concat the end on to it.


RE: Using preg_replace_callback with a href - wolfgang1983 - 10-31-2016

(10-31-2016, 06:34 AM)InsiteFX Wrote: If it's dropping the end of the link do a check and if it's a link concat the end on to it.

How do I do that?


RE: Using preg_replace_callback with a href - InsiteFX - 10-31-2016

PHP Code:
$string .= 'What you need here'

Do that after you check the string and do your replacing.


RE: Using preg_replace_callback with a href - wolfgang1983 - 10-31-2016

(10-31-2016, 02:46 PM)InsiteFX Wrote:
PHP Code:
$string .= 'What you need here'

Do that after you check the string and do your replacing.

I tried that not worked


RE: Using preg_replace_callback with a href - wolfgang1983 - 11-01-2016

Update

I have added it in like this now but now prints it out twice


PHP Code:
$string $this->input->post('question');

$new_data '';

$new_data .= preg_replace_callback("#~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~#", function ($match) {
return 
$match[0] == '<' '&lt;' : ($match[0] == '>' '&gt;' $match[0]);
}, 
$string);

$new_data .= preg_replace_callback("#</?(pre|code|h1|h2|h3|h4|h5|h6|b|strong|i|u|hr)>|[<>]|#", function ($match) {
return 
$match[0] == '<' '&lt;' : ($match[0] == '>' '&gt;' $match[0]);
}, 
$string);

$data['question'] = parse_smileys($new_database_url('assets/img/smiley')); 



RE: Using preg_replace_callback with a href - InsiteFX - 11-01-2016

Try this one:

PHP Code:
$new_data preg_replace_callback("#</?(pre|code|h1|h2|h3|h4|h5|h6|b|strong|i|u|hr)>|[<>]#", function ($match) {
 
   return $match[0] == '<' '<' : ($match[0] == '>' '>' $match[0]);
}, 
$string);

$new_data .= "</a>";

$data['question'] = parse_smileys($new_database_url('assets/img/smiley')); 



RE: [Solved] Using preg_replace_callback with a href - wolfgang1983 - 11-01-2016

I did a couple changes on my script and got it working