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
Share on other sites

thanks for your reply

 

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

for example this code to change *sometext* to <b>sometext</b>.

but its work with 2 * before and after 'sometext'.

and for other tags,i dont know what should i do.really.

Link to comment
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
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?

Edited by AyKay47
Link to comment
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 = "test@example.com"
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
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 = "test@example.com"
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);

Edited by AyKay47
Link to comment
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 = "test@example.com" String mystr = test@@@@::example.com; @ *some text* -some text- Other Image 

it showed me this text.

just Links works right.

Link to comment
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
Share on other sites

First regex for matching:

 

$pattern = '~^@[^@]+\n@~m';

 

For the second search, the pattern I already gave you for it will work. Keep in mind that the regex I am writing is very loose and is meant to get you going, not as an finite answer.

Edited by AyKay47
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.