diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f1c485e..744391a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,6 +13,7 @@ jobs: - '8.0' - '8.1' - '8.2' + - '8.3' name: php ${{ matrix.php-version }} on ${{ matrix.operating-system }} runs-on: ${{ matrix.operating-system }} steps: diff --git a/README.md b/README.md index 3055234..69f0625 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ You can also include the supplied `html2text.php` and use `$text = convert_html_ | Option | Default | Description | |--------|---------|-------------| | **ignore_errors** | `false` | Set to `true` to ignore any XML parsing errors. | -| **drop_links** | `false` | Set to `true` to not render links as `[http://foo.com](My Link)`, but rather just `My Link`. | +| **drop_links** | `false` | Set to `true` to not render links as `[http://foo.com](My Link)`, but rather just `My Link`. Set to `'href'` to return the URL instead of the name.| | **char_set** | `'auto'` | Specify a specific character set. Pass multiple character sets (comma separated) to detect encoding, default is ASCII,UTF-8 | Pass along options as a second argument to `convert`, for example: diff --git a/src/Html2Text.php b/src/Html2Text.php index 1763cb4..0a21985 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -477,7 +477,9 @@ private static function iterateOverNode(\DOMNode $node, ?string $prevName, bool } else { // replace it if ($output) { - if ($options['drop_links']) { + if ($options['drop_links'] === 'href') { + $output = str_replace(' ', '+', $href); + } elseif ($options['drop_links']) { $output = "$output"; } else { $output = "[$output]($href)"; diff --git a/tests/Html2TextTest.php b/tests/Html2TextTest.php index 5e2e522..645ae8c 100644 --- a/tests/Html2TextTest.php +++ b/tests/Html2TextTest.php @@ -38,7 +38,7 @@ function doTestWithResults(string $test, string $result, $options = []): void { } /** @return array> */ - public function providerFiles(): array { + public static function providerFiles(): array { return [ ['basic'], ['anchors'], @@ -91,6 +91,14 @@ public function testAnchorsDropLinks(): void { $this->doTestWithResults("anchors", "anchors.no-links", ['drop_links' => true]); } + public function testBasicHRefLinks(): void { + $this->doTestWithResults("basic", "basic.href-links", ['drop_links' => 'href']); + } + + public function testAnchorsHRefLinks(): void { + $this->doTestWithResults("anchors", "anchors.href-links", ['drop_links' => 'href']); + } + public function testWindows1252(): void { $this->doTestWithResults("windows-1252-example", "windows-1252-example", ['char_set' => 'windows-1252']); } diff --git a/tests/txt/anchors.href-links.txt b/tests/txt/anchors.href-links.txt new file mode 100644 index 0000000..4c29e2f --- /dev/null +++ b/tests/txt/anchors.href-links.txt @@ -0,0 +1,5 @@ +A document without any HTML open/closing tags. +--------------------------------------------------------------- +We try and use the representation given by common browsers of the HTML document, so that it looks similar when converted to plain text. http://foo.com - or http://www.foo.com http://foo.com + +An anchor which will not appear \ No newline at end of file diff --git a/tests/txt/basic.href-links.txt b/tests/txt/basic.href-links.txt new file mode 100644 index 0000000..a5c75ed --- /dev/null +++ b/tests/txt/basic.href-links.txt @@ -0,0 +1,15 @@ +Hello, World! + +This is some e-mail content. Even though it has whitespace and newlines, the e-mail converter will handle it correctly. + +Even mismatched tags. + +A div +Another div +A div +within a div + +Another line +Yet another line + +http://foo.com \ No newline at end of file