In PHP, you can extract numbers from a string using regular expressions and the preg_match_all()
function. Here's an example of how to extract all numbers from a string:
This will output:
Explanation:
/[0-9]+/
searches for one or more consecutive digits.preg_match_all()
function searches the string for all occurrences of the regular expression and stores the matches in the $matches
array.print_r()
ফাংশনটি অ্যারের বিষয়বস্তু প্রদর্শন করতে ব্যবহৃত হয়।আপনি একটি স্ট্রিং থেকে অ-সংখ্যাসূচক অক্ষরগুলি সরাতে এবং শুধুমাত্র সংখ্যাগুলি রাখতে preg_replace()
ফাংশনটি ব্যবহার করতে পারেন:
এটি আউটপুট করবে:
ব্যাখ্যা:
/D/
যেকোনো অ-অঙ্কের অক্ষরের সাথে মেলে।preg_replace()
ফাংশনটি একটি খালি স্ট্রিং দিয়ে সমস্ত নন-ডিজিট অক্ষর প্রতিস্থাপন করে, কার্যকরভাবে স্ট্রিং থেকে তাদের সরিয়ে দেয়।একটি অন্য উপায় আছে তবে এটি উপরের দুটি বিকল্পের মতো নির্ভরযোগ্য নয়। স্ট্রিং থেকে int বের করতে আমরা filter_var()
পিএইচপি বিল্ট-ইন ফাংশন ব্যবহার করতে পারি।
এটি আউটপুট করবে: