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

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];

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

How can I remove a portion of a string from the right side of a string starting at a special character [duplicate]

This question already has answers here:
Break up/parse a URL into its constituent parts in php
(1 answer)
How to remove the querystring and get only the URL?
(16 answers)
Closed 3 years ago.
I have a series of strings created as URLs as the page gets refreshed.I want to remove a portion from the right side of these strings. The strings look like:
http://funfun.ca/?image=1
http://funfun.ca/?image=14
http://funfun.ca/?image=217
and so on. The goal is for anything starting with "?" to be removed from the right side of the strings and leave the following for all of them:
http://funfun.ca/
I appreciate any help I can have to solve this
You may explode the url string for ? and save only the first part obtained:
$firstPart = explode('?', $url)[0];
Example:
echo $firstPart = explode('?', 'http://funfun.ca/?image=1')[0];
returns:
http://funfun.ca/

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

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.

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]');

Need to replace deprecated ereg_replace [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
I am working for a non-profit and i'm not an expert in PHP.
I need to replace the following code:
$status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $status);
When I attempt to modify it to preg_replace, I get an error every different way I try to exit the code.
This will do the job:
$statut = preg_replace('~[a-z]+://[^<>\s]+[\w/]~i', '$0', $statut);
But if the goal of this replacement is to keep all urls and transform them into links, you must change the pattern a little. And, why not, test them with filter_validate_url

Categories