1

I want to create some customised tags for translating, for instance

<trad>SOMETHING</trad>

I've also got a file with some $GLOBALS variable, like:

$GLOBALS['SOMETHING'] = 'Some text';

$GLOBALS['SOMETHINGELSE'] = 'Some other text';

So I've been able to show my translation in this way:

$string = "<trad>SOMETHING</trad>";

$string = preg_replace('/<trad[^>]*?>([\\s\\S]*?)<\/trad>/','\\1', $string);
echo $GLOBALS[$string];

This works perfectly, but when I've got something more complex like the following code, or when I have more occurences of this tag, I'm not able to let it work:

$string = "Lorem ipsum <trad>SOMETHING</trad> <h1>Hello</h1> <trad>SOMETHINGELSE</trad>";

I ideally want to create a new variale $string, replacing the values that I found into my tags and being able to show it with a simple echo.

So I want an output like this with:

echo $string; //output: Lorem ipsum Some text <h1>Hello</h1> Some other text

Can you guys help me?

1
  • You need to use the DOMDocument class and its methods (find a tutorial/read PHP manual). Regex isn't the right way to do that. (You have a structured string = use the structure) Commented May 28, 2017 at 23:47

1 Answer 1

2

Regex is not a valid approach for treating HTMLstring. Here we are using DOMDocument instead of Regex to achieve desired output. The last step of strip_tags has been done to achieve desired output, there will no need in case a valid HTML string is supplied to loadHTML, in that case saveHTML($node) will do the job.

Try this code snippet here

<?php
ini_set('display_errors', 1);

libxml_use_internal_errors(true);

$array["SOMETHING"]="some text";
$array["SOMETHINGELSE"]="some text other";

$string = "Lorem ipsum <trad>SOMETHING</trad> <h1>Hello</h1> <trad>SOMETHINGELSE</trad>";

$domDocument = new DOMDocument();
$domDocument->loadHTML($string,LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);

$results=$domDocument->getElementsByTagName("trad");
do
{
    foreach($results as $result)
    {
        $result->parentNode->replaceChild($domDocument->createTextNode($array[trim($result->nodeValue)]),$result);
    }
}
while($results->length>0);

echo strip_tags($domDocument->saveHTML(),"<h1>");
Sign up to request clarification or add additional context in comments.

6 Comments

but in this way I have to predict with the last instruction all the tags that could be inserted? like h1,h2,h3,a, img, etc..?
@Ame No issues, dont rely on strip_tags($domDocument->saveHTML(),"<h1>"); but just make sure if you dont apply this then output is surrounded by p tag which you can trim, in case when you are not supplying a valid HTML, else it will work fine, You can check it
@Ame You will get the output like this <p>Lorem ipsum Some text <h1>Hello</h1> Some other text</p>, which you can trim.
ok thank you. Is the array approach the only one available? I can't use my GLOBALS variable right?
I'm saying this because I tried to replace $array with $GLOBALS and it doesn't seem to work... if it's easy to solve can you help me? I would be really grateful :)
|

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.