Jump to content

Using Regex


absfrm

Recommended Posts

I have a question to use regex in PHP.

i have a string in murkup.and wanna to convert it to html.

These are my strings:

1. "Text Link":http://site.com

It want to be a html link with url after : and before "space" at the end, and link's text is string between the quotations

 

2. !http://site.com/picture.gif!

It want to be a html img tag: <img src='link between "!" characters' />

 

3. *.my text

It want to be an item in html tag : <li>mytext after "*." and before "enter" (new line)</li>

or etc.

 

how can i do it?

i try many solutions in regex but not worked right.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/270608-using-regex/
Share on other sites

yes.

 

1: "Text Link":http://site.com

It want to be a html link with url after ':' and before 'space' at the end, and link's text is string between the quotations

 

2: !http://site.com/picture.gif!

It want to be a html img tag: <img src='link between two "!" characters' />

 

3: *.my text

It want to be an item in html tag : <li>mytext after "*." and befire "enter" (new line)</li>

 

4: _some text_

it want to be an <i> ' some text between to _' and close with </i>

 

5 : -some text-

it want to be and <s> 'some text between to -' and close with </s>

Link to comment
https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392039
Share on other sites

I understand the parameters, but are all of these sub-strings being searched for in one string or in several strings? I cannot create a regex for you unless I am given a sample string to work with. An example would be:

 

$text = "this is a sample string with text link:http://site.com and other stuff";

 

In the example you provided:

 

$text = preg_replace("/.**{2}(.+?)\*{2}.*/", "<b>$1</b>", $text);

 

What is the string value of $text initially?

Link to comment
https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392045
Share on other sites

oohh man.your are very nice!

this is my $text:

 

$text = 'Just for test ^ sad as dasd ^sa da da sa
"some text":http://google.com
"متن(UTF-8 string)":http://google.com

*.sometext
* sometext

#.some text
# some text

some text !http://static.php.ne...es/php.gif(This is image's Alternative)!
@
String mystr = "[email protected]"
String mystr = test@@@@::example.com;
@
*some text*
_some text_
-some text-

Other Image !http://static.php.net/images/php.gif(This is image's Alternative Text)!
'

 

No Different between '*.' and '* ' both are '<li > < /li>'

No Different between '#.' and '# ' both are <ol></ol>

Link to comment
https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392073
Share on other sites

First, as-is the apostrophe used in images's alternative will break the string and give unexpected results, escape the apostrophe with a backslash \'

I will do a couple of the regex patterns to give you the idea.

 

$text = 'Just for test ^ sad as dasd ^sa da da sa
"some text":http://google.com
"متن(UTF-8 string)":http://google.com


*.sometext
* sometext


#.some text
# some text


some text !http://static.php.ne...es/php.gif(This is image\'s Alternative)!
@
String mystr = "[email protected]"
String mystr = test@@@@::example.com;
@
*some text*
_some text_
-some text-


Other Image !http://static.php.net/images/php.gif(This is image\'s Alternative Text)!';
$patterns = array();
$patterns[] = '~"([^"]+)":(http://[^\n]+)~i';
$patterns[] = '~!(http://[^(]+)\(([a-z\' ]+)\)!~i';
$patterns[] = '~\*(?:\.|\s)?([a-z ]+)$~im';
$patterns[] = '~_([^_]+)_~i';
$replace = array();
$replace[] = '<a href=\'$2\'>$1</a>';
$replace[] = '<img src=\'$3\' alt=\'$4\'/>';
$replace[] = '<li>$5</li>';
$replace[] = '<i>$6</i>';
echo preg_replace($patterns, $replace, $text);

Link to comment
https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392092
Share on other sites

Just for test ^ sad as dasd ^sa da da sa some text متن(UTF-8 string) *.sometext * sometext #.some text # some text some text  @ String mystr = "[email protected]" String mystr = test@@@@::example.com; @ *some text* -some text- Other Image 

it showed me this text.

just Links works right.

Link to comment
https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392119
Share on other sites

To make strong

$regex = "#([]*[])(.*)([]*[])#e";
$text = preg_replace($regex,"('<strong>$2</strong>')",$text);

 

To Make Images

$regex2 = "#([]![])(.*)([(])(.*)([]![])#e";
$text = preg_replace($regex2,"('<img src=\"$2\" alt=\"\">')",$text);

 

To Make Italic

$regex5 = "#([]_[])(.*)([]_[])#e";
$text = preg_replace($regex5,"('<i>$2</i>')",$text);

 

To Make Strick

$regex6 = "#([-])(.*)([-])#e";
$text = preg_replace($regex6,"('<s>$2</s>')",$text);

 

To put <br > for each empty line.

$text = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "<br>", $text);

 

Now i want to do these actions :

@
some code
@

 

Change first '@' with <pre > and last '@' to </ pre>

i can't detect that character is the first character of line!

if both '@' are first character of line,then do change it

 

And change if Line start with '*.' or '* '(with space after it) , and end with 'enter' or '\n' or '<br>'(end line)

change start , with <li > and end with </li >

Link to comment
https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392354
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.