Viewed   82 times

I found a routine to create a html link when a link is found in a text

 function makelink($text) 
 return preg_replace('/(http://[a-zA-Z0-9_-.]*?) /i', '<a href="$1">$1</a> ', $text." "); 

 // works
 echo makelink ("hello how ");

 // dont work
 echo makelink ("hello how ");


as you see in the example, it works find with a domain only, not when there is a page or subdirectory is within that link.

Can you provide a solution for that function to work also with pages and subdirs?

thx chris



The characters ?=& are for urls with query strings. Notice that I changed the separator from / to ! because there a lot of slashes in your expression. Also note that you don't need A-Z if you are in case-insensitive mode.

return preg_replace('!(http://[a-z0-9_./?=&-]+)!i', '<a href="$1">$1</a> ', $text." ");
Friday, November 25, 2022
  1. There is a problem with the url you try to access. It is broken ! You should have tried first. The new URL, that I found on the FF console is :

    For the single word Hello. And you see that you have to specify the language, and the length of your text in textlen, even though it did work for all the sentences I tried without changing this var.

  2. Another problem is that you have to urlencode() your text, or you will have a bug with accents and punctuation. So the line to download the MP3 becomes :

    // Language of the sentence
    $lang = "fr";
    $mp3 = file_get_contents(
    ''. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');

So the complete code looks like :


    $text = "Bonjour, comment allez vous ?";
    // Yes French is a beautiful language.
    $lang = "fr";

    // MP3 filename generated using MD5 hash
    // Added things to prevent bug if you want same sentence in two different languages
    $file = md5($lang."?".urlencode($text));

    // Save MP3 file in folder with .mp3 extension 
    $file = "audio/" . $file . ".mp3";

    // Check folder exists, if not create it, else verify CHMOD
    if (!is_dir("audio/"))
        if (substr(sprintf('%o', fileperms('audio/')), -4) != "0777")
            chmod("audio/", 0777);

    // If MP3 file exists do not create new request
    if (!file_exists($file))
        // Download content
        $mp3 = file_get_contents(
        ''. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
        file_put_contents($file, $mp3);

Friday, November 11, 2022

The PHP part actually works for me, though this could be replaced:

$data = ob_get_clean();

with this:

imagepng($im, $to);

But I'm not positive if that's the issue, since your existing code worked for me. So try checking if the $paths is a valid directory?


nothing seems to be happening

Could it be because you didn't echo (or output something) after the image is created? Well I mean, the image may have been successfully (re-)created, but your PHP/JS script doesn't "tell" you of the status; hence you see "nothing". Try echo-ing something like this:

if (isset($_POST['make_pic'])) {

  echo 'Success!';

And then use this JS:

  success: function(res){

(Update #2: I intentionally removed the "Additional Note" part, but you can always see the answer's revisions to find that part.)

Wednesday, August 10, 2022

Well you'd read the CSV file into a multidimensional array.

Consider that each line in the CSV file is now a column (goes up to down instead of left to right). This is called Transposing rows to columns.

For a table you'll need to loop through each row, not each column. So you create a loop within a loop as shown here:

<table border="0" cellspacing="1" cellpadding="1" class="sortable" border="1"><caption>Title Here</caption>
     <thead><tr><th class="header">Time:</th><th class="header">Value 1:</th><th class="header">Value 2:</th><th class="header">Value 3:</td class="header"><th class="header">Value 4:</th><th class="header">Value 5:</th><th class="header">Value 6:</th><th class="header">Value 7:</th><th class="header">Value 8:</th><th class="header">Value 9:</th></tr></thead><tbody>
     #read CSV file
     if (($handle = fopen("data.csv", "r")) !== FALSE) {
       $mycsv = array();
       while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) $mycsv[] = $data;

     #Find the length of the transposed row

     $row_length = count($mycsv);

     #Loop through each row (or each line in the csv) and output all the columns for that row
     foreach($mycsv[0] as $col_num => $col)
        echo "<tr>";
        for($x=0; $x<$row_length; $x++)
           echo "<td>".$mycsv[$x][$col_num]."</td>";

        echo "</tr>";


Try that out and let me know if it works.

Friday, August 12, 2022

For example, I have the following HTML, where all the words are lowercase:

    <h2>page not found!</h2>
    <p>go to <a href="/">home page</a> or use the <a href="/search">search</a>.</p>

My task is to convert text to capitalized words. To solve it, I fetch all text nodes and convert them using the ucwords function (of course, you should use your translation function instead of it).

$dom = new DomDocument();
$xpath = new DOMXPath($dom);

foreach ($xpath->query('//text()') as $text) {
    if (trim($text->nodeValue)) {
        $text->nodeValue = ucwords($text->nodeValue);

echo $dom->saveHTML();

The above outputs the following:

    <h2>Page Not Found!</h2>
    <p>Go To <a href="/">Home Page</a> Or Use The <a href="/search">Search</a>.</p>
Tuesday, October 25, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :