EDIT: I've made the following changes since first posting this code:
- Streamlined usage somewhat by merging methods clear() and URL() into do(), which sets
$this->sURLand clears values of other properties. - Changed "anchor" to "ID" after realizing that HTML 5 has replaced anchors with element IDs.
- Provided for default value of link text. If text() is not called, then the URL is used as the link text.
- Included generated output as comment at the end of the code.
.
<?php declare(strict_types = 1);
$oLink = new link;
class link
{private $sURL;
private $sText;
private $sID;$sAnchor;
private $sQuery;
private $sWindow;
public function doclear(string $sURL)
{$this->sURL = $sURL;'';
$this->sText = '';
$this->sID >sAnchor = '';
$this->sQuery = '';
$this->sWindow = '';
return $this;
}
public function text URL(string $sText$sURL) {$this->sText>sURL = $sText;$sURL; return $this;}
public function ID text(string $sID$sText) {$this->sID>sText = $sText; = $sID; return $this;}
public function anchor(string $sAnchor) {$this->sAnchor = $sAnchor; return $this;}
public function query (string $sQuery) {$this->sQuery = $sQuery; return $this;}
public function window(string $sWindow) {$this->sWindow = $sWindow; return $this;}
public function hLink():string
{$this->sURLreturn('<a =HRef="' . $this->sURL .
($this->sQuery == '' ? '' : '?' . $this->sQuery) .
($this->sID ($this->sAnchor == '' ? '' : '#' . $this->sID>sAnchor);
.
if($this->sText == '') $this->sText = $this->sURL;
return('<a HRef="' . $this->sURL . '"' . ($this->sWindow == '' ? '' : ' target="' . $this->sWindow . '"') .
'>' .
'>' . $this->sText .
'</a>');
}
}
echo('<p>Link to ' . $oLink->do>clear()->URL('http://example.com')->text('website')->ID>anchor('section')->query('input=0')->window('_blank')->hLink() . ' in text</p>');
echo("\r\n");
echo('<p>Link to ' . $oLink->do('http://example.com')->ID('section')->query('input=0')->hLink() . ' in text</p>');
echo("\r\n");
echo('<p>Link to ' . $oLink->do('http://example.com')->hLink() . ' in text</p>');
/* Output:
I've tested the above code as a freestanding script, and it does output the intended:
<p>Link to <a HRef="http://example.com?input=0#section" target="_blank">website</a> in text</p>
<p>Link to <a HRef="http://example.com?input=0#section">http://example.com?input=0#section</a> in text</p>
<p>Link to <a HRef="http://example.com">http://example.com</a> in text</p>
*/
I've tested the above code as a freestanding script, and it's output exactly matches the comment at the end: