Not sure if anyone is a familiar with CKEditor, but it's an HTML WSYIWYG editor that can be added to a website to modify content. With CKEditor you can do a lot of different things along with making links. When you click the link button, you have a few options, [url]http://,[/url] [url]ftp://,[/url] [url]https://,[/url] [url]news://,[/url] and other(for internal links)

I'm trying to develop a way without trying to edit the CKEditor javascript, to make it so when the user chooses other(meaning [url]http://,[/url] [url]ftp://,[/url] [url]https://,[/url] or news:// doesn't exist) that it adds a php variable to the beginning of the link.

Ex:

<a href="test1">Test Page</a>

reformed to

<a href="<?php echo $baseDirectory; ?>test1>Test Page</a>

I've accomplished this by doing:


$pattern = "/href=/"; 
$test= preg_replace($pattern, '<a href="<?php echo $baseDirectory; ?>', $test);
$test = str_replace('<a href="<?php echo $baseDirectory; ?>\"', '<a href="<?php echo $baseDirectory; ?>', $test);
$test = str_replace('\"', '"', $test);

But when I choose another option other than other it adds it to it as well so:

<a href="http://google.com">Google</a>

gets reformed to

<a href=<?php echo $baseDirectory; ?>http://google.com</a>

I need it so it only adds it to the other option, not the [url]http://,[/url] [url]ftp://,[/url] [url]https://,[/url] news:// options.

    To disable news:: would likely best be done in the javascript code for CKEditor. Try opening up the javascript files in a text editor and searching for 'news'.

    Or you could parse the HTML resulting from a form submission using php's [man]preg_replace[/man] function.

    I tend to think this should be done in javascript because what happens when some edits a given link twice? You add it the first time automatically and then when they open it the second time they might think "what the heck is that extra garbage?"

    Any chance you could perform this var addition at the display phase rather than the edit phase?

      I don't want to be able to disable the [url]http://,[/url] news:// [url]https://,[/url] or the ftp:// feature. I just want to make it so if they choose the other option, the base directory gets added to the beginning of the link.

      I agree that it may be easier and more efficient to make these changes in the javascript, but #1 I am not that familiar with javascript and #2, all the javascript is condensed meaning all the code is in one big block and impossible to figure out(at least for me).

      The thought of the user updating the field already crossed my mind and I was planning on doing exactly what you suggested, modifying the link in the text on the display, when the information is outputted from the database, instead of on inserting into the database.

      I also agree that the preg_replace() function is what I need to use, because I've tried str_replace() and that didn't work out too well. But I am not sure how to use preg_replace() in this case.

      Someone suggested that I only preg_replace() where the pattern does not equal [url]http://,[/url] [url]https://,[/url] [url]ftp://,[/url] or news:// which would make sense that it would work, but how they suggested to accomplish this was incorrect.

      $pattern = "/<a href=[^(http|ftp|https|news|)]/";
      

        Have you considered using a base tag? This instructs the browser to evaluate all relative links relative to some specific domain and path.

        if that doesn't work, you can try searching your text with this regex:

        $pattern = '/(<a[^>]+href=")((?!http|ftp|news)[^"]+)("[^>]*>)/i';
        $count = preg_match_all($pattern, $string_to_search, $matches);
        print_r($matches);
        

        That pattern should also work in a preg_match and will separate out the various parts of the A tag for manipulation.

          Write a Reply...