diff --git a/.travis.yml b/.travis.yml
index 0d95f3a..952f91d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,10 +1,9 @@
language: php
php:
- - 5.4
- - 5.5
- - 5.6
+ - '7.0'
+ - '7.1'
script:
- - bin/behat
+ - vendor/bin/phpunit
before_script:
- composer self-update
- composer install
diff --git a/composer.json b/composer.json
index 22da7f6..100bf84 100644
--- a/composer.json
+++ b/composer.json
@@ -10,18 +10,15 @@
"ext-curl": "*"
},
"require-dev": {
- "behat/behat": "2.4.*@stable",
- "phpunit/phpunit": "4.6.*"
+ "phpunit/phpunit": "~6.3"
},
"bin": [
"doc-parser"
],
- "config": {
- "bin-dir": "bin/"
- },
"autoload": {
"psr-4": {
- "DocParser\\": ["src/"]
+ "DocParser\\": ["src/"],
+ "DocParser\\Tests\\": ["tests/"]
}
},
"autoload-dev": {
diff --git a/doc-parser.php b/doc-parser.php
index c41055c..a67685d 100644
--- a/doc-parser.php
+++ b/doc-parser.php
@@ -29,8 +29,10 @@
// Setup cli commands
$runCmd = new \DocParser\Command\RunCommand();
+$singleCmd = new \DocParser\Command\SingleCommand();
$application = new \Symfony\Component\Console\Application();
$application->add($runCmd);
+$application->add($singleCmd);
$application->setDefaultCommand($runCmd->getName());
$application->run();
\ No newline at end of file
diff --git a/features/availability.feature b/features/availability.feature
deleted file mode 100644
index 29c7438..0000000
--- a/features/availability.feature
+++ /dev/null
@@ -1,12 +0,0 @@
-Feature: availability
- In order to choose which languages for PHP manual
- As a parser script
- I want to download php.net/download-docs.php and list available languages
-
- Scenario: Run the CLI tool parser.php which lets you choose the languages you want
- When I want a list all available languages
- Then I should get these and more:
- | lang | title |
- | en | English |
- | es | Spanish |
- | de | German |
\ No newline at end of file
diff --git a/features/bootstrap/AvailabilityContext.php b/features/bootstrap/AvailabilityContext.php
deleted file mode 100644
index b815b13..0000000
--- a/features/bootstrap/AvailabilityContext.php
+++ /dev/null
@@ -1,42 +0,0 @@
-languages = $avail->listPackages();
- }
-
- /**
- * @Then /^I should get these and more:$/
- */
- public function iShouldGetTheseAndMore(TableNode $table)
- {
- foreach ($table->getHash() as $row) {
- assertArrayHasKey($row['lang'], $this->languages, 'Language ' . $row['lang'] . ' must be among available languages.');
- assertEquals($row['title'], $this->languages[$row['lang']]);
- }
- foreach ($this->languages as $code => $title) {
- assertNotEmpty($title);
- }
- assertGreaterThan(10, count($this->languages), 'There are missing languages for sure!');
- }
-
-}
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
deleted file mode 100644
index 60a07ad..0000000
--- a/features/bootstrap/FeatureContext.php
+++ /dev/null
@@ -1,21 +0,0 @@
-useContext('availability_subcontext_alias', new AvailabilityContext());
- $this->useContext('package_subcontext_alias', new PackageContext());
- $this->useContext('parser_subcontext_alias', new ParserContext());
- $this->useContext('parser_result_subcontext_alias', new ParserResultContext());
- $this->useContext('utils_subcontext_alias', new UtilsContext());
- }
-
-}
\ No newline at end of file
diff --git a/features/bootstrap/PackageContext.php b/features/bootstrap/PackageContext.php
deleted file mode 100644
index ce3d554..0000000
--- a/features/bootstrap/PackageContext.php
+++ /dev/null
@@ -1,70 +0,0 @@
-package = new Package($lang, $mirror);
- }
-
- /**
- * @Then /^The package can be downloaded to system\'s tmp dir$/
- */
- public function thePackageCanBeDownloadedToSystemSTmpDir()
- {
- $this->dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-doc-parser-test';
- @mkdir($this->dir);
-
- $file = $this->dir . DIRECTORY_SEPARATOR . $this->package->getOrigFilename();
-
- $this->package->download($file);
- }
-
- /**
- * @Given /^Unpack files to the same directory:$/
- */
- public function unpackFilesToTheSameDirectory(PyStringNode $string)
- {
- $expectedFiles = $string->getLines();
- $this->pagesDir = $this->package->unpack($expectedFiles);
- assertFileExists($this->pagesDir);
-
- chdir($this->pagesDir);
- $foundFiles = array_flip(glob('*.html'));
- assertCount(count($expectedFiles), $foundFiles);
-
- foreach ($expectedFiles as $file) {
- assertArrayHasKey($file, $foundFiles);
- }
- }
-
- /**
- * @Given /^Cleanup files when it\'s all done$/
- */
- public function cleanupFilesWhenItSAllDone()
- {
- $this->package->cleanup();
- assertFileNotExists($this->pagesDir, 'Directory "' . $this->pagesDir . '" wasn\'t removed.');
- }
-
-}
diff --git a/features/bootstrap/ParserContext.php b/features/bootstrap/ParserContext.php
deleted file mode 100644
index 3a3e47f..0000000
--- a/features/bootstrap/ParserContext.php
+++ /dev/null
@@ -1,106 +0,0 @@
-parser = new Parser();
-// var_dump($this->testFilesDir);exit;
- }
-
- /**
- * @Given /^these test files:$/
- */
- public function theseTestFiles(TableNode $table)
- {
- $this->testFiles = $table->getHash();
- }
-
- /**
- * @Then /^match them with files from "([^"]*)"$/
- */
- public function matchThemWithFilesFrom($dir)
- {
- $this->testFilesDir = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR . $dir;
-
- foreach ($this->testFiles as $row) {
- $expected = json_decode(file_get_contents($this->testFilesDir . DIRECTORY_SEPARATOR . $row['expected-json-result']), true);
-
- $result = $this->parser->processFile($this->testFilesDir . DIRECTORY_SEPARATOR . $row['source-filename'], Parser::INCLUDE_EXAMPLES);
- $funcName = $result->getFuncNames()[0];
-
- $actual = array_merge($result->getResult($funcName), [
- 'examples' => $result->getExamples($funcName)
- ]);
-
-// echo json_encode($actual, JSON_PRETTY_PRINT);
-
- assertEquals($expected, $actual);
- }
- }
-
- /**
- * @Then /^download manual from "([^"]*)" package from "([^"]*)"$/
- */
- public function downloadManualFromPackageFrom($lang, $mirror)
- {
- $this->package = new Package($lang, $mirror);
-
- $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-doc-parser-test';
- @mkdir($dir);
- $file = $dir . DIRECTORY_SEPARATOR . $this->package->getOrigFilename();
-
- $this->package->download($file);
- $testUnpackDir = $this->package->unpack(array_map(function($item) { return $item['source-filename']; }, $this->testFiles));
-
- $this->unpackedFilesDir = $this->package->getUnpackedDir();
-
- assertEquals($testUnpackDir, $this->unpackedFilesDir);
- assertCount(count($this->testFiles), glob($this->unpackedFilesDir . DIRECTORY_SEPARATOR . '*.html'));
- }
-
- /**
- * @Given /^test downloaded files against them as well$/
- */
- public function testDownloadedFilesAgainstThemAsWell()
- {
-// $files = glob($this->downloadedFilesDir . DIRECTORY_SEPARATOR . '*.html');
-
- foreach ($this->testFiles as $row) {
- $file = $row['source-filename'];
- $expectedFileSize = filesize($this->unpackedFilesDir . DIRECTORY_SEPARATOR . $file);
- $testFileSize = filesize($this->testFilesDir . DIRECTORY_SEPARATOR . $file);
-
- if ($expectedFileSize != $testFileSize) {
- echo "File sizes doesn't match for \"${file}\"! Documentation might not match the tested sample.\n";
- }
-
-// $actual = $this->parser->processFile($this->testFilesDir . DIRECTORY_SEPARATOR . $file);
- }
-
- $this->package->cleanup();
- }
-}
\ No newline at end of file
diff --git a/features/bootstrap/ParserResultContext.php b/features/bootstrap/ParserResultContext.php
deleted file mode 100644
index 1b584a4..0000000
--- a/features/bootstrap/ParserResultContext.php
+++ /dev/null
@@ -1,84 +0,0 @@
-getHash() as $row) {
- $result = new ParserResult();
- $file = $row['test-file'];
- $result->setResult($file, $row['result']);
- if ($row['examples']) {
- foreach (explode(', ', $row['examples']) as $example) {
- $result->addExample($file, $example);
- }
- }
-
-// $result->addWarning($file, $row['warning'] ? explode(', ', $row['warning']) : []);
- if ($row['warning']) {
- foreach (explode(', ', $row['warning']) as $warning) {
- $result->addWarning($file, $warning);
- }
- }
-
- if ($row['skip']) {
- $result->addSkipped($file);
- }
-
- if (isset($this->results[$file])) {
- $this->results[$file]->mergeWithResult($result);
- } else {
- $this->results[$file] = $result;
- }
- }
- }
-
- /**
- * @Then /^after merging them I\'m expecting one result with:$/
- */
- public function afterMergingThemIMExpectingOneResultWith(TableNode $table)
- {
- $result = new ParserResult();
- foreach ($this->results as $r) {
- $result->mergeWithResult($r);
- }
-
- $warningsCount = $examplesCount = 0;
- foreach ($table->getHash() as $row) {
- $file = $row['test-file'];
-
- $warningsCount += count(explode(', ', $row['warning']));
- $examplesCount += count(explode(', ', $row['examples']));
-
- assertEquals(explode(', ', $row['warning']), $result->getWarnings($file));
- assertEquals(explode(', ', $row['examples']), $result->getExamples($file));
- assertEquals($row['result'], $result->getResult($file));
- if ($row['skip']) {
- assertTrue($result->isSkipped($file));
- } else {
- assertFalse($result->isSkipped($file));
- }
- }
-
- assertEquals($warningsCount, $result->countAllWarnings());
- assertEquals($examplesCount, $result->countAllExamples());
- }
-
-}
\ No newline at end of file
diff --git a/features/bootstrap/UtilsContext.php b/features/bootstrap/UtilsContext.php
deleted file mode 100644
index 0aa18f7..0000000
--- a/features/bootstrap/UtilsContext.php
+++ /dev/null
@@ -1,42 +0,0 @@
-testArrays = $table->getHash();
- }
-
- /**
- * @Then /^I need to convert it recursively to a low level more efficient SplFixedArray$/
- */
- public function iNeedToConvertItRecursivelyToALowLevelMoreEfficientSplfixedarray()
- {
- /**
- * Probably not very helpful
- */
- foreach ($this->testArrays as $json) {
- $array = json_decode($json['test-array-as-json'], true);
-// $fixed = Utils::toFixedArray($array);
-// var_dump(memory_get_peak_usage(true) / 1000000);
- }
-
- }
-
-}
\ No newline at end of file
diff --git a/features/package.feature b/features/package.feature
deleted file mode 100644
index 4ce8fa9..0000000
--- a/features/package.feature
+++ /dev/null
@@ -1,19 +0,0 @@
-Feature: package
- In order to parse the documentation
- As a parser script
- I need to download and unpack the manual to a temporary directory
-
- Scenario:
- When I need "fr" manual from "cz1.php.net"
- Then The package can be downloaded to system's tmp dir
- And Unpack files to the same directory:
- """
- class.pdo.html
- class.splheap.html
- datetime.diff.html
- function.cos.html
- function.is-string.html
- function.print-r.html
- function.strcmp.html
- """
- Then Cleanup files when it's all done
\ No newline at end of file
diff --git a/features/parser-result.feature b/features/parser-result.feature
deleted file mode 100644
index c1a585f..0000000
--- a/features/parser-result.feature
+++ /dev/null
@@ -1,16 +0,0 @@
-Feature: ParserResult
- In order to work with parser
- As a developer
- I need to merge multiple parser results
-
- Scenario:
- When I have multiple parser results like:
- | test-file | warning | skip | result | examples |
- | file1 | msg1 | | res1 | ex1, ex2 |
- | file2 | msg2, msg3 | 1 | res2 | ex4 |
- | file1 | | | res1 | ex3 |
- | file2 | msg4 | | res4 | |
- Then after merging them I'm expecting one result with:
- | test-file | warning | skip | result | examples |
- | file1 | msg1 | | res1 | ex1, ex2, ex3 |
- | file2 | msg2, msg3, msg4 | 1 | res4 | ex4 |
\ No newline at end of file
diff --git a/features/parser.feature b/features/parser.feature
deleted file mode 100644
index 9f797bb..0000000
--- a/features/parser.feature
+++ /dev/null
@@ -1,20 +0,0 @@
-Feature: Parser
- In order to get a manual page in JSON format
- As a developer
- I need to run series of XPath queries that process the HTML page
-
- Scenario:
- Given these test files:
- | source-filename | expected-json-result |
- | datetime.setdate.html | datetime.setdate.json |
- | eventhttp.setcallback.html | eventhttp.setcallback.json |
- | function.array-diff.html | function.array-diff.json |
- | function.chown.html | function.chown.json |
- | function.json-encode.html | function.json-encode.json |
- | function.str-replace.html | function.str-replace.json |
- | function.strrpos.html | function.strrpos.json |
- | reflectionclass.getname.html | reflectionclass.getname.json |
- | splfileobject.fgetcsv.html | splfileobject.fgetcsv.json |
- Then match them with files from "test-manual-files"
- Then download manual from "en" package from "cz1.php.net"
- And test downloaded files against them as well
diff --git a/features/utils.feature b/features/utils.feature
deleted file mode 100644
index a82bec2..0000000
--- a/features/utils.feature
+++ /dev/null
@@ -1,11 +0,0 @@
-Feature: Utils
- In order to do some common things when parsing manual pages
- As a developer
- I need to have a set of static methods that can be used independently
-
- Scenario:
- When I have a very large multi-dimensional array:
- | test-array-as-json |
- | [ "a", "b", ["c", ["d"]], "e"] |
- | { "k1": "a", "k2": { "k3": [ 1, 2, 3 ] }, "k4": [ 4, 5 ] } |
- Then I need to convert it recursively to a low level more efficient SplFixedArray
\ No newline at end of file
diff --git a/phpunit.xml b/phpunit.xml
new file mode 100644
index 0000000..5912aeb
--- /dev/null
+++ b/phpunit.xml
@@ -0,0 +1,7 @@
+
', '
', '
'), "\n", $source))));
if (substr($source, 0, 5) == 'length == 0) {
@@ -94,7 +76,8 @@ public static function extractFormattedText($elms, $xpath = null) {
return self::simplifyString(implode('\n\n', $textParts));
}
- static public function convertSize($size) {
+ static public function convertSize($size)
+ {
// http://stackoverflow.com/questions/11807115/php-convert-kb-mb-gb-tb-etc-to-bytes
$number = substr($size, 0, -1);
switch(strtoupper(substr($size,-1))){
@@ -108,15 +91,4 @@ static public function convertSize($size) {
return $size;
}
}
-
-// static public function toFixedArray($array) {
-// $fixed = \SplFixedArray::fromArray($array, true);
-//
-// foreach ($fixed as $key => $value) {
-// if (is_array($value)) {
-// $fixed[$key] = self::toFixedArray($value);
-// }
-// }
-// return $fixed;
-// }
}
diff --git a/tests/AvailabilityTest.php b/tests/AvailabilityTest.php
new file mode 100644
index 0000000..0c67485
--- /dev/null
+++ b/tests/AvailabilityTest.php
@@ -0,0 +1,23 @@
+ 'English',
+ 'es' => 'Spanish',
+ 'de' => 'German',
+ ];
+
+ $avail = new Availability();
+ $languages = $avail->listPackages();
+
+ $this->assertArraySubset($expected, $languages);
+ }
+}
\ No newline at end of file
diff --git a/tests/PackageTest.php b/tests/PackageTest.php
new file mode 100644
index 0000000..d820c59
--- /dev/null
+++ b/tests/PackageTest.php
@@ -0,0 +1,46 @@
+assertEquals('php_manual_en.tar.gz', $package->getOrigFilename());
+ }
+
+ public function testGetUrl()
+ {
+ $package = new Package('en', 'cz1.php.net');
+ $this->assertEquals('http://cz1.php.net/get/php_manual_en.tar.gz/from/this/mirror', $package->getUrl());
+ }
+
+ public function testUnpack()
+ {
+ $expectedFiles = [
+ 'class.pdo.html',
+ 'class.splheap.html',
+ 'datetime.diff.html',
+ 'function.cos.html',
+ 'function.is-string.html',
+ 'function.print-r.html',
+ 'function.strcmp.html',
+ ];
+
+ $package = new Package('en', 'cz1.php.net');
+ $tmpFile = $package->download();
+ $dir = $package->unpack($expectedFiles);
+
+ $unpackedFiles = scandir($dir);
+ foreach ($expectedFiles as $file) {
+ $this->assertContains($file, $unpackedFiles);
+ }
+
+ $package->cleanup();
+ $this->assertFileNotExists($tmpFile);
+ }
+}
\ No newline at end of file
diff --git a/tests/ParserResultTest.php b/tests/ParserResultTest.php
new file mode 100644
index 0000000..3362cd3
--- /dev/null
+++ b/tests/ParserResultTest.php
@@ -0,0 +1,83 @@
+setResult($file, $row[3]);
+ if ($row[4]) {
+ foreach (explode(',', $row[4]) as $example) {
+ $result->addExample($file, trim($example));
+ }
+ }
+
+ if ($row[1]) {
+ foreach (explode(',', $row[1]) as $warning) {
+ $result->addWarning($file, trim($warning));
+ }
+ }
+
+ if ($row[2]) {
+ $result->addSkipped($file);
+ }
+
+ if (isset($allResults[$file])) {
+ $allResults[$file]->mergeWithResult($result);
+ } else {
+ $allResults[$file] = $result;
+ }
+ }
+
+ // Check results
+ $mergedResult = new ParserResult();
+ foreach ($allResults as $r) {
+ $mergedResult->mergeWithResult($r);
+ }
+
+ $warningsCount = $examplesCount = 0;
+ foreach ($expected as $row) {
+ $file = $row[0];
+
+ $warningsCount += count(explode(',', $row[1]));
+ $examplesCount += count(explode(',', $row[4]));
+
+ $this->assertEquals(array_map(function($s) { return trim ($s); }, explode(',', $row[1])), $mergedResult->getWarnings($file));
+ $this->assertEquals(array_map(function($s) { return trim ($s); }, explode(',', $row[4])), $mergedResult->getExamples($file));
+ $this->assertEquals($row[3], $mergedResult->getResult($file));
+ if ($row[2]) {
+ assertTrue($mergedResult->isSkipped($file));
+ } else {
+ assertFalse($mergedResult->isSkipped($file));
+ }
+ }
+
+ $this->assertEquals($warningsCount, $mergedResult->countAllWarnings());
+ $this->assertEquals($examplesCount, $mergedResult->countAllExamples());
+ }
+
+}
\ No newline at end of file
diff --git a/tests/ParserTest.php b/tests/ParserTest.php
new file mode 100644
index 0000000..c1f9121
--- /dev/null
+++ b/tests/ParserTest.php
@@ -0,0 +1,42 @@
+ 'datetime.setdate.json',
+ 'eventhttp.setcallback.html' => 'eventhttp.setcallback.json',
+ 'function.array-diff.html' => 'function.array-diff.json',
+ 'function.chown.html' => 'function.chown.json',
+ 'function.json-encode.html' => 'function.json-encode.json',
+ 'function.str-replace.html' => 'function.str-replace.json',
+ 'function.strrpos.html' => 'function.strrpos.json',
+ 'function.strtr.html' => 'function.strtr.json',
+ 'reflectionclass.getname.html' => 'reflectionclass.getname.json',
+ 'splfileobject.fgetcsv.html' => 'splfileobject.fgetcsv.json',
+ ];
+
+ $parser = new Parser();
+
+ foreach ($expectedFiles as $file => $jsonFile) {
+ $parserResult = $parser->processFile(self::TEST_FILES_DIR . DIRECTORY_SEPARATOR . $file);
+ $expected = json_decode(file_get_contents(self::TEST_FILES_DIR . DIRECTORY_SEPARATOR . $jsonFile), true);
+
+ $funcName = $parserResult->getFuncNames()[0];
+ $actual = $parserResult->getResult($funcName);
+ $actual['examples'] = $parserResult->getExamples($funcName);
+
+ $this->assertEquals($expected, $actual);
+ }
+ }
+
+}
diff --git a/features/test-manual-files/datetime.setdate.html b/tests/test-manual-files/datetime.setdate.html
similarity index 100%
rename from features/test-manual-files/datetime.setdate.html
rename to tests/test-manual-files/datetime.setdate.html
diff --git a/features/test-manual-files/datetime.setdate.json b/tests/test-manual-files/datetime.setdate.json
similarity index 100%
rename from features/test-manual-files/datetime.setdate.json
rename to tests/test-manual-files/datetime.setdate.json
diff --git a/features/test-manual-files/eventhttp.setcallback.html b/tests/test-manual-files/eventhttp.setcallback.html
similarity index 100%
rename from features/test-manual-files/eventhttp.setcallback.html
rename to tests/test-manual-files/eventhttp.setcallback.html
diff --git a/features/test-manual-files/eventhttp.setcallback.json b/tests/test-manual-files/eventhttp.setcallback.json
similarity index 100%
rename from features/test-manual-files/eventhttp.setcallback.json
rename to tests/test-manual-files/eventhttp.setcallback.json
diff --git a/features/test-manual-files/function.array-diff.html b/tests/test-manual-files/function.array-diff.html
similarity index 100%
rename from features/test-manual-files/function.array-diff.html
rename to tests/test-manual-files/function.array-diff.html
diff --git a/features/test-manual-files/function.array-diff.json b/tests/test-manual-files/function.array-diff.json
similarity index 100%
rename from features/test-manual-files/function.array-diff.json
rename to tests/test-manual-files/function.array-diff.json
diff --git a/features/test-manual-files/function.chown.html b/tests/test-manual-files/function.chown.html
similarity index 100%
rename from features/test-manual-files/function.chown.html
rename to tests/test-manual-files/function.chown.html
diff --git a/features/test-manual-files/function.chown.json b/tests/test-manual-files/function.chown.json
similarity index 100%
rename from features/test-manual-files/function.chown.json
rename to tests/test-manual-files/function.chown.json
diff --git a/features/test-manual-files/function.json-encode.html b/tests/test-manual-files/function.json-encode.html
similarity index 100%
rename from features/test-manual-files/function.json-encode.html
rename to tests/test-manual-files/function.json-encode.html
diff --git a/features/test-manual-files/function.json-encode.json b/tests/test-manual-files/function.json-encode.json
similarity index 100%
rename from features/test-manual-files/function.json-encode.json
rename to tests/test-manual-files/function.json-encode.json
diff --git a/features/test-manual-files/function.str-replace.html b/tests/test-manual-files/function.str-replace.html
similarity index 100%
rename from features/test-manual-files/function.str-replace.html
rename to tests/test-manual-files/function.str-replace.html
diff --git a/features/test-manual-files/function.str-replace.json b/tests/test-manual-files/function.str-replace.json
similarity index 100%
rename from features/test-manual-files/function.str-replace.json
rename to tests/test-manual-files/function.str-replace.json
diff --git a/features/test-manual-files/function.strrpos.html b/tests/test-manual-files/function.strrpos.html
similarity index 100%
rename from features/test-manual-files/function.strrpos.html
rename to tests/test-manual-files/function.strrpos.html
diff --git a/features/test-manual-files/function.strrpos.json b/tests/test-manual-files/function.strrpos.json
similarity index 100%
rename from features/test-manual-files/function.strrpos.json
rename to tests/test-manual-files/function.strrpos.json
diff --git a/tests/test-manual-files/function.strtr.html b/tests/test-manual-files/function.strtr.html
new file mode 100644
index 0000000..0b1cb82
--- /dev/null
+++ b/tests/test-manual-files/function.strtr.html
@@ -0,0 +1,220 @@
+
+
+
(PHP 4, PHP 5, PHP 7)
strtr — Translate characters or replace substrings
+ +$str
+ , string $from
+ , string $to
+ )$str
+ , array $replace_pairs
+ )
+ If given three arguments, this function returns a copy of
+ str where all occurrences of each (single-byte)
+ character in from have been translated to the
+ corresponding character in to, i.e., every
+ occurrence of $from[$n] has been replaced with
+ $to[$n], where $n is a valid
+ offset in both arguments.
+
+ If from and to have
+ different lengths, the extra characters in the longer of the two
+ are ignored. The length of str will be the same as
+ the return value's.
+
+ If given two arguments, the second should be an array in the + form array('from' => 'to', ...). The return value is + a string where all the occurrences of the array keys have been + replaced by the corresponding values. The longest keys will be tried first. + Once a substring has been replaced, its new value will not be searched + again. +
+
+ In this case, the keys and the values may have any length, provided that
+ there is no empty key; additionally, the length of the return value may
+ differ from that of str.
+ However, this function will be the most efficient when all the keys have the
+ same size.
+
+
str+ The string being translated. +
+from
+ The string being translated to to.
+
to
+ The string replacing from.
+
replace_pairs
+ The replace_pairs parameter may be used instead of
+ to and from, in which case it's an
+ array in the form array('from' => 'to', ...).
+
+ Returns the translated string. +
+
+ If replace_pairs contains a key which
+ is an empty string (""),
+ FALSE will be returned. If the str is not a scalar
+ then it is not typecasted into a string, instead a warning is raised and
+ NULL is returned.
+
+
Example #1 strtr() example
+
+<?php
//In this form, strtr() does byte-by-byte translation
//Therefore, we are assuming a single-byte encoding here:
$addr = strtr($addr, "äåö", "aao");
?>
+
++ The next example shows the behavior of strtr() when + called with only two arguments. Note the preference of the replacements + ("h" is not picked because there are longer matches) + and how replaced text was not searched again. +
+Example #2 strtr() example with two arguments
+
+<?php
$trans = array("h" => "-", "hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
?>
+
+The above example will output:
+hello all, I said hi +
+ The two modes of behavior are substantially different. With three arguments, + strtr() will replace bytes; with two, it may replace + longer substrings. +
+Example #3 strtr() behavior comparison
+
+<?php
echo strtr("baab", "ab", "01"),"\n";
$trans = array("ab" => "01");
echo strtr("baab", $trans);
?>
+
+The above example will output:
+1001 +ba01 +
+