taylornetwork/make-html 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

taylornetwork/make-html

Composer 安装命令:

composer require taylornetwork/make-html

包简介

A trait for to convert plain text to html, inlcuding links.

README 文档

README

This is a class and trait for Laravel that will convert text with line breaks and links into HTML. It also adds simple HTML tag generation as well as a helper function for imploding associative arrays.

Install

Via Composer

$ composer require taylornetwork/make-html

Dependencies

TaylorNetwork\MakeHTML will install TaylorNetwork\LaravelHelpers. Laravel should auto discover that package, but if you run into problems you may need to include the provider manually.

Setup

This package is set up with auto discovery so you should be good to go out of the box. Check Manual Setup below if something isn't working.

Manual Setup

If auto discovery doesn't work...

Add the service provider to the providers array in config/app.php.

'providers' => [
    TaylorNetwork\MakeHTML\MakeHTMLServiceProvider::class,
];

Publishing

Run

php artisan vendor:publish

This will add makehtml.php to your config directory.

Usage

You can use this package either by including the trait in a class you want to implement the functionality or by creating a new instance of the class.

Trait

Import the trait into your class.

use TaylorNetwork\MakeHTML\MakeHTML;

class DummyClass
{
    use MakeHTML;

    // Code
}

The MakeHTML trait includes two functions, usage examples below.

makeHTML (string $text)

Accepts one parameter, the text you want the generator to parse.

Given this example variable $text

$text = 'Example text
with line
breaks. And a
link: http://example.com/page/1/2?q=This&that=other';

Calling makeHTML

$this->makeHTML($text);

Returns

'Example text<br>with line<br>breaks. And a<br>link: <a href="http://example.com/page/1/2?q=This&that=other">example.com</a>'

getHTMLGeneratorInstance ()

Accepts no parameters and returns an instance of HTMLGenerator class, or creates one.

$this->getHTMLGeneratorInstance();

You can chain any methods from the HTMLGenerator class onto that to return functionality.

See class usage below for examples.

Class

Instantiate the class.

use TaylorNetwork\MakeHTML\HTMLGenerator;

$instance = new HTMLGenerator();

Available Methods

makeLinks (string $text)

Converts any found links in the string to clickable links with <a> tag.

Will take the base URL as the caption for the link.

For example:

$textWithLink = 'I have a link http://example.com/page/1/2/3?query=string';

Call makeLinks($textWithLink)

$instance->makeLinks($textWithLink);

Returns

'I have a link <a href="http://example.com/page/1/2/3?query=string">example.com</a>'
convertLineEndings (string $text)

Converts all line endings to HTML <br> tags.

$textWithLineBreaks = 'This
is
text
with
line
breaks.';

Call convertLineEndings($textWithLineBreaks)

$instance->convertLineEndings($textWithLineBreaks);

Returns

'This<br>is<br>text<br>with<br>line<br>breaks.'
makeHTML (string $text)

Calls convertLineEndings($text) and makeLinks($text) and returns the converted text.

generateTag (string $tag, array $attributes, boolean $closeTag = true)

Will generate an HTML tag with any attributes given.

To generate a <div> tag with no attributes

$instance->generateTag('div', []);

Returns

'<div></div>'

To generate a <div> tag with attributes

$attributes = [ 'class' => 'example-class second-class third' 'data-attr' => 'value' ];

Call function

$instance->generateTag('div', $attributes);

Returns

'<div class="example-class second-class third" data-attr="value"></div>'

To generate a <div> tag with attributes and also data between the tags, add the external attribute and everything there will be added between the tags.

$attributes = [
    'class' => 'example-class',
    'id' => 'div1',
    'name' => 'some-name',
    'external' => 'This is external data!',
];

Call function

$instance->generateTag('div', $attributes);

Returns

'<div class="example-class" id="div1" name="some-name">This is external data!</div>'

To generate a <div> tag and keep it open, to process more external data for example.

$instance->generateTag('div', $attributes, false);

Returns

'<div class="example-class" id="div1" name="some-name">This is external data!'

closeTag(string $tag)

Closes a given tag

$instance->closeTag('div');

Returns

'</div>'

Magic Method

You can also call generateTag($tag, $attributes) by calling the tag you want followed by Tag

To generate a <div> tag this way

$instance->divTag($attributes);

Would call generateTag('div', $attributes) for you.

Config

The config file once published is in config/makehtml.php

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 55
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-11-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固