Convert sting to html code using Laravel or PHP [duplicate] - php

This question already has answers here:
PHP Regex markdown from string
(1 answer)
Why is my PHP regex that parses Markdown links broken?
(2 answers)
PHP regex. Convert [text](url) to text [duplicate]
(1 answer)
Closed 4 years ago.
I have the following string :
this is sample text
[abc def.pdf](https://example.com/post/abc def.pdf)
This is another line after file upload
[dfg.pdf](https://example.com/post/dfg.pdf)
![IMG_3261.JPG](https://example.com/post/IMG_3261.JPG)
this are some other line append to sting.
I want to convert this string in to as follow.
this is sample text
abc def.pdf
This is another line after file upload
dfg.pdf
<img src="https://example.com/post/IMG_3261.JPG"/ title="IMG_3261.JPG">
this are some other line append to sting.
How could I do it using laravel or php?
I know I can do that using php function preg_replace but I am not good at REGEXP. So please help me to do it.
what i try so far
i try this solution
Why is my PHP regex that parses Markdown links broken?
But it will only converting to anchor tag but i also want to convert it to image tag too
preg_replace('#((https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?))#', '$1', $string);
but it will not get output i mention above. How can i do it.

Related

How to prevent PHP from renaming '&times' to 'x' symbol? [duplicate]

This question already has answers here:
Why is "&reg" being rendered as "®" without the bounding semicolon
(8 answers)
Should an ampersand be URL encoded in a query string?
(2 answers)
Closed 2 years ago.
I have with an unwanted replaced, and not able to figure out how to fix it.
When you echo the following string in PHP
echo('?hash=123&rid=111&timestamp=123');
The output is:
?hash=123&rid=111×tamp=123
Note that &timestamp has been replaced with ×tamp
I tried to escape it by using \&timestamp but that doens't work.
How can I prevent PHP replacing this?
You can reproduce this error online using http://phptester.net/
You have to escape that string, because & is a special symbol in HTML.
echo htmlspecialchars('?hash=123&rid=111&timestamp=123');
More information on the PHP site: https://www.php.net/manual/en/function.htmlspecialchars.php

find/replace in PHP string [duplicate]

This question already has answers here:
PHP strtr vs str_replace benchmarking
(3 answers)
Replace text in a string using PHP
(3 answers)
Closed 5 years ago.
I have a PHP string in which I would like to find and replace using the strtr function, problem is I have variable fields so I won't be able to replace by name. The string contains tags like the following:
[field_1=Company]
[field_4=Name]
What makes it difficult is the "Company" and "Name" part of the "tag", these can be variable. So I basically looking for a way to replace this part [field_1] where "=Company" and "=Name" must be discarded. Can this be done?
To explain: I'm using "=Company" so users don't just see "field_1" but know the value it represents. However users are able to change the value to what they see fit.
You are probably looking for regular expressions. There is a function in PHP to do a regex replace:
http://php.net/manual/en/function.preg-replace.php
Been a while since I've worked in PHP but you might want to try something like this:
preg_replace('/field_\d/','REPLACEMENT','[field_1=Company]');
Should result in
[REPLACEMENT=Company]
If you want to replace everything except the brackets:
preg_replace('/field_\d+=\w+/','REPLACEMENT','[field_1=Company]');

How to find a character using index in PHP string? [duplicate]

This question already has answers here:
Finding a character at a specific position of a string
(5 answers)
Closed 5 years ago.
I have a string in PHP code like this:
$string = "This is a PHP code";
I want to find a character at index 3 in above string. And output must be:
s
Is there any idea to achieve this goal ?
If I understood your question properly, you want character at 3rd index then write following line ;)
echo $string[3];

PHP to remove xml [duplicate]

This question already has answers here:
How to get the value of an attribute from XML file in PHP?
(4 answers)
Closed 6 years ago.
I need some help with a problem if possible.
<IndexFile index="dlc:blahblahblahblah.zip" version="1.19.0" />
How would I use php to remove everything in the above line of code except
blahblahblahblah.zip
Note: blahblah isn't actual name, the name changes; I just need to remove the xml on either side of the .zip file.
I've tried a few things like strip_tags() but nothing works up to now.
Your test string was
<IndexFile index="dlc:blahblahblahblah.zip" version="4.19.0" />
Your regex pattern should be : index="dlc:(.+?)".
Your answer is:
blahblahblahblah.zip
Try it out at https://regex101.com/
See this answer for greedy vs nongreedy matching: java-pattern-does-not-return-leftmost-match
Ideally, you have to use an XML parser to parse the file and use XPaths/looping to get that item.
If that is the only line, why can't you just use a Regex to extract the value? index="[a-zA-Z0-9_.-:]*" (or [a-zA-Z0-9_.-:]*.zip)?
Again, you need to think about the future impacts.

How to read txt file interpreting the \n? [duplicate]

This question already has answers here:
How can I replace newline or \r\n with <br/>?
(10 answers)
Closed 7 years ago.
how to read a .txt file showing the html tag and interpreting the \n in the file.
I'm using a script php to read those files:
echo readfile("file.txt");
thanks.
You are looking for nl2br function.
echo nl2br(readfile("file.txt"));

Categories