absfrm Posted November 12, 2012 Share Posted November 12, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/ Share on other sites More sharing options...
AyKay47 Posted November 12, 2012 Share Posted November 12, 2012 Can you post the last solution you have tried so we can steer you in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1391910 Share on other sites More sharing options...
absfrm Posted November 13, 2012 Author Share Posted November 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1391959 Share on other sites More sharing options...
AyKay47 Posted November 13, 2012 Share Posted November 13, 2012 If you could, post a sample string where the regex would need to search for these patterns. Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392034 Share on other sites More sharing options...
absfrm Posted November 13, 2012 Author Share Posted November 13, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392039 Share on other sites More sharing options...
AyKay47 Posted November 13, 2012 Share Posted November 13, 2012 (edited) 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 November 13, 2012 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392045 Share on other sites More sharing options...
absfrm Posted November 13, 2012 Author Share Posted November 13, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392073 Share on other sites More sharing options...
AyKay47 Posted November 13, 2012 Share Posted November 13, 2012 (edited) 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 November 13, 2012 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392092 Share on other sites More sharing options...
absfrm Posted November 13, 2012 Author Share Posted November 13, 2012 i could fix images by array. and for each character , i detected those and changed with html tags and its work now with alternative text! ok special thanks to you sir. Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392108 Share on other sites More sharing options...
AyKay47 Posted November 13, 2012 Share Posted November 13, 2012 glad it worked for you, please mark this topic as solved. Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392113 Share on other sites More sharing options...
absfrm Posted November 13, 2012 Author Share Posted November 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392119 Share on other sites More sharing options...
absfrm Posted November 14, 2012 Author Share Posted November 14, 2012 $regex = "#([]*[])(.*)([]*[])#e"; preg_replace($regex,"('<b>$2</b>')",$text); i could fix *sometext* by above regex. this changed the start * with <b > and end tag with </b > Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392189 Share on other sites More sharing options...
AyKay47 Posted November 14, 2012 Share Posted November 14, 2012 post all of the relevant code that you are using now. Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392230 Share on other sites More sharing options...
absfrm Posted November 14, 2012 Author Share Posted November 14, 2012 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 > Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392354 Share on other sites More sharing options...
AyKay47 Posted November 14, 2012 Share Posted November 14, 2012 (edited) 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 November 14, 2012 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392390 Share on other sites More sharing options...
absfrm Posted November 16, 2012 Author Share Posted November 16, 2012 thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/270608-using-regex/#findComment-1392912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.