"Dialectizer" in PHP.. fun project :D

Posted on 2004-11-24
Last Modified: 2008-01-09
OK, so I am trying to make my own Dialectizer type thing (based on the concept behind )

I am usually pretty PHP competent, but this time I have failed.

Basically what I need it to do, is get a URL, and replace any instance of abc with xyz. I got that far.

Heres are the things I can't figure out (not yet anyway),
It must convert all relative image and CSS links to absolute links pointing to where they should
It has to point all relative links through the dialectizer as well

So, I thought I would throw it out here and see if anyone could help me out with the code..

Heres my code so far:

if ($_GET['url']) {
// grab out put from page
$pagecontent = ob_get_contents();

// modify output
$pagecontent = eregi_replace("http://" , 'index.php?url=http://', $pagecontent);
$pagecontent = eregi_replace("ing", "in'", $pagecontent);
$pagecontent = eregi_replace("and", "an'", $pagecontent);
$pagecontent = eregi_replace("for", "fo'", $pagecontent);
$pagecontent = eregi_replace("broken", "busted", $pagecontent);
$pagecontent = eregi_replace("what", "whut", $pagecontent);
$pagecontent = eregi_replace("ever", "evah", $pagecontent);
$pagecontent = eregi_replace("tion", "shun", $pagecontent);
$pagecontent = eregi_replace("potatoe", "tater", $pagecontent);

// write output to page
echo $pagecontent;
} else {
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<form method="get" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<input name="url" type="text" /><br />
<input name="submit" type="submit" value="Dialectize It!" />

<?php } ?>

Question by:JoshPowell
    LVL 25

    Expert Comment

    For a start I'd suggest that you build arrays of match patterns and replacements so you can do it all in one pass. It will be much faster:

    $in=array('ing', 'and', 'for'); //etc
    $out = array('in', 'an', 'fo');
    $translation = preg_replace($in, $out, $pagecontent);

    You only need to redirect <a> tag links through your script, so your search for "http" etc is too wide. Try:

    $pagecontent = preg_replace('/(<\s*a\s+.*href\s*=\s*")(.*)(")/i', '$1index.php\?url=$2"$3', $pagecontent);

    This also copes with links where the href is not the first attribute in an a tag, varying spacing, case insentitive.

    You still need to convert images and CSS style or link meta tags to absolute URLs, along with form submission actions - I'll leave that for someone else, but you might find parse_url() and realpath() useful.
    LVL 3

    Author Comment

    Thanks.. I will read up on those functions :)

    LVL 3

    Author Comment

    So does anybody know how to convert relative style/image URLs in to absolute =P

    LVL 3

    Accepted Solution

    Heh, I looked at this at work today and wrote a partial solution, but.. guess it doesn't matter now.

    function callback_abhref($matches)
     global $scriptpath;
     echo "abhref: {$matches[1]}<br>";
     return $scriptpath . urlencode($matches[1]) . '"';
    function callback_href($matches)
     global $folder,$scriptpath;
     echo "href: {$matches[1]}<br>";
     if (strpos($scriptpath,$matches[1])==1)
      return "FISH";
     return $scriptpath . urlencode($folder . $matches[1]) . '"';

    function callback_roothref($matches)
     global $domain,$scriptpath;
     echo "roothref: {$matches[1]}<br>";
     return $scriptpath . urlencode($domain . $matches[1]) . '"';
    <a href="">Link text</a>
    <a href="/index.php">Link text</a>
    <a href="fish/monkey?page=fish">Link text</a>

    echo "<pre>" . htmlspecialchars($a);
    LVL 3

    Author Comment


    I didn't mean to abandon this.. I was just waiting for someone to awnser :P

    Thanks for your help krazywest , I will play around with that code :)

    LVL 3

    Expert Comment

    Just so you know, the problem I was having was that strpos("string","stringmonkeybob") was returning FALSE instead of 0 (like it should).

     if (strpos($scriptpath,$matches[1])==1)
      return "FISH";

    Change that to:

     if (strpos($scriptpath,$matches[1])===0)
      return $matches[1];

    And then debug it. ;)
    LVL 3

    Author Comment

    Ah, thanks :)

    You have proven most helpfull.. I shall start work on my script now :D


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now