www.webdeveloper.com
Results 1 to 3 of 3

Thread: preg_replace, ignore <pre></pre>

  1. #1
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192

    preg_replace, ignore <pre></pre>

    Hi all,

    I'm working with the Joomla! CMS and I'm trying to write a simple SEO script which converts keywords to links. It's easy enough, but I'm having an issue with it.

    I have GeSHi highlighting throughout my articles using the Joomla! GeSHi library, and sometimes the highlighted code gets ruined with a big juicy href because a keyword appears inside the <pre xml:lang=""></pre> tags.

    So basically, in Joomla! you've got $this->item->text, which is the full article text. I'm using this code for my SEO:

    keywords.php (included in Joomla! core file)
    PHP Code:
    $seo_keywords = array 
    (
        
    "PHP" => "http://www.php.net",
        
    "JavaScript" => "http://www.ecmascript.org",
        
    "CSS" = > "http://www.w3.org"
    );

    $limit 1// Limit how many keywords can become links.

    foreach($seo_keywords as $key=>$value)
    {    
        
    // preg_replace(text to replace, replacement value, subject, limit)
        
        
    $this->item->text 
                
    preg_replace("/\b" $key"\b/i"
                
    "<a href='" $value "'>" $key "</a>"
                
    $this->item->text
                
    $limit);

    I would like this regular expression to be identical to what it is now, but to ignore all text that appears between <pre xml:lang="some_language"> and </pre>.

    Can anyone help me out with the necessary expression?

    Notes:
    1. I know there are Joomla! extensions that do auto-SEO, but the reason I decided to write my own code is because those extensions also replace unwanted segments of text with links.
    2. These are not my actual keywords.

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    I think that you need to rethink doing this with regular expressions. Better to use DOMDocument or XMLParser. XMLPArser, which parses HTML as well, will be more work but will offer the more elegant solution in the end.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Charles,

    I'm not sure I understand where you're coming from. I've just quickly Googled DOMDocument and XML Parser, but I don't have a clue what they are or do. Is this what you're refering to? http://php.net/manual/en/class.domdocument.php

    If so, how can it help me? I've never heard of it before and the php.net page doesn't seem to have much explanation other than code examples in the comments.

    Also, why is my method not a good way of adding links automatically? Seems simple enough from my perspective. I'm not trying to create a Joomla! add-on or extension, just quick, simple automated links to various pages and articles around my own site (not external links).

    I'm a little confused.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles