0

ORIGINAL CODE

$sentance="are you hungry too?";
function newLanguage($text) {
	$sql = "SELECT in,out FROM words";
	$res = mysql_query($sql) or die();
	$in_array = array();
	$out_array = array();
	while ($row = mysql_fetch_array($res)){ 
		$in_array[] = $row['in']; // table in, NEW words
		$out_array[] = $row['out']; //table out, ENG words
	}
	return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
echo $newwords;

AMENDED CODE:

ini_set("display_errors", "1"); 
error_reporting(E_ALL);

function newLanguage($text) {

$sql = "SELECT in,out FROM words";
$res = mysql_query($sql) or die();
$in_array = array();
$out_array = array();
while ($row = mysql_fetch_array($res)){ 
	$in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
	$out_array[] = $row['out']; //table out, ENG words
}

/* VERSION 2 - STATIC, FOR DEBUGGING
$in_array = array('~\you~s','~\to~s','~\too~s');
$out_array = array('noa','nie','niee');*/

return preg_replace($in_array,$out_array,$text);
}
$sentance="are you hungry too?";
$newwords = newLanguage($sentance);

var_dump($in_array);
echo $sentance;

CURRENT CODE

ini_set("display_errors", "1"); 
error_reporting(E_ALL);

$sentance="are you hungry too?";
function newLanguage($text) {

$sql = "SELECT * FROM words";
$res = mysql_query($sql) or die();		
while ($row = mysql_fetch_array($res)){ 
    $in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
    $out_array[] = $row['out']; //table out, ENG words
}
return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
var_dump($in_array);
echo $newwords;

I'm having Problems with my code, for the life of me I cannot get it to work, I have worked with preg_replace before in using a word filter. Though creating a dynamic array using query results totally throws me off. I have looked at a few tutorials but none have really helped me understand where I am going wrong.

Any help would be grateful :)

GOAL: Creating a New Language translation. Database holds a row with 'in' & 'out' which is both the new language and local language.

PROBLEM: I'm unsure if my Arrays are being successfully populated since my preg_replace isn't working.

----UPDATE----

Here is what my database looks like;

id          in          out
1           you         noa
2           to          nie
3           too         niee

  • They are stored as VARCHARS
5
  • Are your in values really regular expressions, or just plain strings? Commented Dec 23, 2014 at 9:47
  • Can you post sample values from the words table? Make sure you have error_reporting(E_ALL) enabled? You're probably getting errors from preg_replace, because the regexp doesn't have proper delimiters on it. Commented Dec 23, 2014 at 9:48
  • How do I Enable error_reporting? I haven't encountered this before. Do I simply insert; ini_set("display_errors", "1"); error_reporting(E_ALL); above my query? Commented Dec 23, 2014 at 10:05
  • Yes, that's how you do it. Commented Dec 23, 2014 at 10:06
  • Okay, that's in. Still nothing new to report though as I'm not getting any errors though I think I have found what IS the problem though I'm unsure of how to fix it. When I removed $newwords = newLanguage($sentance); I was able to get sentance to echo. Though with $newwords = newLanguage($sentance); in the code it wont even echo sentance.. something isn't right. Commented Dec 23, 2014 at 10:12

2 Answers 2

1

Try this:

while ($row = mysql_fetch_array($res)){ 
    $in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
    $out_array[] = $row['out']; //table out, ENG words
}

This turns the in worods into regular expressions, adding \b to match word boundaries.

My whole test code is:

<?php
$sentance="are you hungry too?";
function newLanguage($text) {
    $in_array = array();
    $out_array = array();
    $rows = array(array('in' => 'you', 'out' => 'noa'),
                  array('in' => 'to', 'out' => 'nie'),
                  array('in' => 'too', 'out' => 'niee'));
    foreach ($rows as $row) {
        $in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
        $out_array[] = $row['out']; //table out, ENG words
    }
    return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
echo $newwords;

The $rows array replaces the database query, but the rest is essentially the same.

Sign up to request clarification or add additional context in comments.

13 Comments

Updated, still not working. I'm feeling very stupid right now :( and demotivated. Sorry for wasting your time and I appreciate you taking the time to help.
Please add var_dump($in_array) to your question.
Are you going to add me the output of the var_dump? That's what I wanted to see.
How do I see the output of var_dump? I cannot see it in the console using chrome, how do I see? I dont see Errors nor is the var_dump echoing any output. This surely means that my query is not working or the array is not being built?
This is getting very tiresome, it seems like you're just cutting and pasting with no idea what anything means.
|
0

BIG thank you to Barmar for his dedicated help and patience in helping me solve my question! I have made his answer correct since he helped me so much but if you wish to use my final code that is commented please feel free.

My Goal was to IMPORT database rows and insert them into an Array which will be used with preg_replace.

My Final Code was:

//this is the base text that will be modified using database information
$sentance="are you hungry too?";

function newLanguage($text) {
    $sql = "SELECT * FROM words";
    $res = mysql_query($sql) or die();	
    $in_array = array();
    $out_array = array();
    while ($row = mysql_fetch_array($res)){
        // inserts the column row 'in' into an array called in_array
        $in_array[] = '/\b' . preg_quote($row['in']) . '\b/';
        // inserts the column row 'out' into an array called out_array
        $out_array[] = $row['out']; 
	}
    //replaced any matching words from 'sentance' with 'in_array' and replaces with 'out_array'
    return preg_replace($in_array,$out_array,$text);
}

//this makes a new variable and used a function to replace the text with matches in the variable
$newwords = newLanguage($sentance); 
echo $newwords;

6 Comments

You're still missing the initializations of the arrays.
It works and I have No errors. I add the initializations and I have errors. Thank you for your help though :) I really appreciate it!
That makes absolutely no sense. You got the notice about an uninitialized variable before because you were missing the initialization.
I added it due to the answer above due to the fact you mentioned it should be in there but I don't have it in my code and it works :) P.S Thank you for being so patient, helping people isn't easy, its time consuming and frustrating but you really made me happy :) so thank you!
The initializations should be BEFORE THE LOOP, not INSIDE it. You're totally clueless.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.