• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Using preg_replace_callback with a href

#1
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));

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
If it's dropping the end of the link do a check and if it's a link concat the end on to it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(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?
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

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

Do that after you check the string and do your replacing.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(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


Attached Files Thumbnail(s)
   
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#6
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')); 
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#7
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')); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
I did a couple changes on my script and got it working
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.