Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand Down
10 changes: 9 additions & 1 deletion tests/Html2TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function doTestWithResults(string $test, string $result, $options = []): void {
}

/** @return array<array<string>> */
public function providerFiles(): array {
public static function providerFiles(): array {
return [
['basic'],
['anchors'],
Expand Down Expand Up @@ -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']);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/txt/anchors.href-links.txt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/txt/basic.href-links.txt
Original file line number Diff line number Diff line change
@@ -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